Search code examples
carraysuint32-t

How to iterate through arrays to copy them into another array of type uint32_t?


I have created a C program that consists of an integer array structure states[2] . I also need a uint32_t type array called store. I just want to copy the contents of the array states[0] into store[0] and the contents of states[1] into store[1]. I used this approach for char arrays and it seemed to work. My code looks like this:

 #include <stdlib.h>
 #include <stdio.h>

 int main()
 {
     uint32_t *store[2];
     int i;
     int *states[2];
     int states[0] = {1,0,0,1};
     int states[1] = {0,0,0,2};

     for (i = 0; i < 3; i++)
     {
        store[i] = states[i];
        i++;
     }
 }

The code however does not execute and says the arrays that I have declared are of invalid format.I am not sure if this is the right approach to do this. Could somebody please help me out with this.


Solution

  • I've re-written your example - forcing the sizes of the arrays - in this, it works.

    EDIT - Addition of printf calls to show the contents of array store.

     #include <stdlib.h>
     #include <stdio.h>
    
     int main()
     {
         int store[3][4];
         int i;
         int states[3][4] = {{1,0,0,1},{0,0,0,2}};
    
         for(i=0; i<3; i++)
         {
            printf( "store[%d] = ", i );
            for( int inner = 0; inner < 4; inner++ )
            {
               store[i][inner] = states[i][inner];
               printf( "%d ", store[i][inner] ); 
            }
            printf( "\n" );
         }
    
        return( 0 );   
    

    }

    When creating pointers in arrays you really need to allocate and then copy.