why wont this complie?
Code:int main() { typedef struct { char block[4][4]; }BLOCK; BLOCK L_block; L_block.block = {0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0,}; }
why wont this complie?
Code:int main() { typedef struct { char block[4][4]; }BLOCK; BLOCK L_block; L_block.block = {0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0,}; }
Because you're trying to do an assignment, not an initialisation.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
hmm
so how do i assign values to an array in a structure?
[edit]
nevermind. what you said made me realise what i was doing wrong
thanks salem.
[/edit]
[edit again]
there must be an easier way to do it than this right? the numbers arent sequential so i cant use a for loop
[/edit again]Code:int main() { typedef struct blk{ char block[4][4]; }BLOCK; BLOCK Lblock[4]; Lblock[0].block[0][0] = 0; Lblock[0].block[0][1] = 1; Lblock[0].block[0][2] = 0; ...//etc return 0; }
Last edited by sand_man; 08-22-2004 at 09:27 AM.
What about
Code:BLOCK Lblock[4] = { 0 }; for ( i = 0 ; i < 4 ; i++ ) Lblock[i].block[0][1] = 1;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Wouldn't that be easier?Code:int main(void) { typedef struct { char block[4][4]; }BLOCK; BLOCK L_block = {0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0,}; return 0; }
Another option, since the array is within a structure, is to use structure assignment.Code:struct sBlock { char block[4][4]; }; static const struct sBlock DefaultBlock = { { {0,1,0,0}, {0,1,0,0}, {0,1,0,0}, {0,1,0,0}, }, }; int main(void) { struct sBlock myblock = {0}; /* initialization */ /* ... */ myblock = DefaultBlock; /* assignment */ return 0; }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
thankyou dave