0

Basically I have this Problem like in C decribed here for Structured Text.

So in C I can do this to copy vector c into matrix rows a :

int a[100][100];
int c[10][10];

int i;
for(i = 0; i<10; i++)
{
    memcpy(&a[i], &c[i], sizeof(c[0]));
}

How to do this in Structured Text? My analogous Approach does not work so far. (Compiler Error: to less indices for field a).

VAR 
      a: ARRAY[0..99,0..99] OF REAL; (*2D array*)
      c : ARRAY[0..99] OF REAL; (*1D array*)
END_VAR

FOR i:=0 TO 99 DO
      memcpy(ADR(a[i]), ADR(c[i]), SIZEOF(c[0]));
END_FOR
Richard Teubner
  • 303
  • 2
  • 11
  • I have an idea but I afraid that problem is different. Looks like you try to solve the task the other way. Can you describe (edit) what you are trying to accomplish? May be there is a completely different approach to it using pointers. – Sergey Romanov Jun 29 '18 at 06:59
  • I like to copy 1D arrays of same size into an 2D array. I also thought about a struct. If you know how to iterate over member of a struct, this is also ok for me.Isn't this above already a pointer approach? – Richard Teubner Jun 29 '18 at 11:15
  • What is IDE? Codesys? Pointers are not a part of IEC61131-3 specification, so everyone implements it differently. Also I cannot understand how you copy one 0-99 array to 0-9801 array which is tootal elements of 0-99,0-99 – Sergey Romanov Jul 01 '18 at 06:19

2 Answers2

0

Are you trying to copy c into a?

For the a array, you need both indexes, like this:

memcpy(ADR(a[i,0]).....

Please test. I believe this is how I remember it, but not by my computer.

Scott
  • 116
  • 2
0

If I understand well, you would like to copy 1 dimension array (1x99 = 99 elements) to a 99 dimension array (99x99 = 9801 elements). You can copy the first array (1 dimension) in the first row o column (or vicerversa), the second in the second... etc.

If this is your porpouse you can try this code:

VAR

    i: INT; //auxiliar
    j: INT; //auxiliar

    origin : ARRAY[0..9] OF REAL;       //origin data
    destiny: ARRAY[0..9,0..9] OF REAL;  //destiny

END_VAR

FOR i := 0 TO 9 DO

    FOR j := 0 TO 9 DO

        //Copy the origin array to the first column, second, etc of destiny array
        destiny[i,j] := origin[i];

    END_FOR;    

END_FOR;

I've tested it on my computer (using codesys) and it works, but I don't know if is this you are looking for...

ada
  • 1
  • 1