Skip to main content
gamedev.net
🔒 Locked

Array question...

Started by alberto_balsalm Nov 25, 2001 at 12:52 PM 1 replies 200+ views
Original Post
alberto_balsalm
alberto_balsalm
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?
Thrump
Thrump
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);

Oluseyi
Oluseyi
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));

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.