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_VARFOR i:=0 TO 99 DOmemcpy(ADR(a[i]), ADR(c[i]), SIZEOF(c[0]));END_FOR
2

Best Answer


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.

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:

VARi: INT; //auxiliarj: INT; //auxiliarorigin : ARRAY[0..9] OF REAL; //origin datadestiny: ARRAY[0..9,0..9] OF REAL; //destinyEND_VARFOR i := 0 TO 9 DOFOR j := 0 TO 9 DO//Copy the origin array to the first column, second, etc of destiny arraydestiny[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...