0

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.

Goldengirl
  • 957
  • 4
  • 10
  • 30

2 Answers2

2

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.

Neil
  • 1,036
  • 9
  • 18
1

There are two issue in your code,

First,

int *states[2];
int states[0] = {1,0,0,1};
int states[1] = {0,0,0,2};

are problematic. While accessing a variable, type is not to be mentioned, it is required only at definition. so, using int states[0] or ..[1] is invalid.

Then, secondly,

states[0] = {1,0,0,1};

state[0] is of type int *, and you're trying to initialize the same with a brace-enclosed initializer list of ints. That is not correct thing, either.

You can modify your code to remove the types while accessing the array elements and to use of compound literal, finally looking something like below

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>   //added for uint32_t 

 int main(void)        //corrected signature
 {
     uint32_t *store[2];
     int i;
     int *states[2];
     states[0] = (int []){1,0,0,1};     //compound literal, C99 and above
     states[1] = (int []){0,0,0,2};

     for (i = 0; i < 2; i++)           //bound corrected
     {
        store[i] = states[i];
                                       //i++; remove this, you're incrementing twice
     }
 }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • thank you for that neat explanation and code :) Could you alos tell me at the end how could I access/print the array in store[0] and store[1]. When I try printing the array in store[0] I get the value 1 instead of the whole array.. – Goldengirl Aug 04 '15 at 10:43
  • 1
    @Goldengirl You'll be needing a loop to print _all_ the contents of the array pointed by `store[n]`. – Sourav Ghosh Aug 04 '15 at 10:45