totally stumped

This is a discussion on totally stumped within the C Programming forums, part of the General Programming Boards category; 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, ...

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    totally stumped

    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,};
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    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
    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;
    }
    [/edit again]
    Last edited by sand_man; 08-22-2004 at 09:27 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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;
    }
    Wouldn't that be easier?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    thankyou dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  2. Can someone help me im totally stumped
    By Joe123 in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2005, 12:17 PM
  3. Writing a "Protocol" - Stumped.
    By mrpickle in forum Game Programming
    Replies: 8
    Last Post: 01-21-2004, 06:37 PM
  4. help with stl search/find, or something else- stumped!
    By Terranc in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2002, 03:11 PM
  5. Everyone look at my totally sweet spaceship
    By Shadow12345 in forum Game Programming
    Replies: 4
    Last Post: 09-30-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21