How to Memcpy into a Two Dimensional Array in Structured Text?

How to Memcpy into a Two Dimensional Array in Structured Text?

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
3

2 Answers

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:

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.