Array question...

Started by
1 comment, last by alberto_balsalm 22 years, 4 months ago
ok.. here''s a simple one. i want to do something like this..
  
struct A
{
    int b[4][4];
};

int main()
{
    A b;
    b.b={{0,0,0,0},
         {0,1,1,1},
         {1,1,1,1},
         {1,1,1,1}};
}
  
I know I can initiate the structure when i declare it, but I want to change the values like above. how do i do that?
Advertisement
I don''t think you can do that. You could write a function that takes a pointer to your var and 16 values...


Stuff(&b.b, 0,0,0,0,            1,1,0,0,            0,0,1,1,            0,0,1,0); 
You could memcpy from a linear array to your structure. Arrays are guaranteed to be contiguous regions of memory. You could even make it easier by using a union to simultaneously define the struct as both a 2D and linear array:
struct A{  union  {    int l[16];    int b[4][4];  };}; 


You can now do this:
A b;int data[] = {0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1};memcpy(b.l, data, 16 * sizeof(int)); 

This topic is closed to new replies.

Advertisement