whats wrong with this code? (using arrays)

This is a discussion on whats wrong with this code? (using arrays) within the C++ Programming forums, part of the General Programming Boards category; Here is the code: Code: struct LEVELINFO { int level[BLOCK_GRID_ROWS][BLOCK_GRID_COLUMNS]; }; int lev1[BLOCK_GRID_ROWS][BLOCK_GRID_COLUMNS] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    whats wrong with this code? (using arrays)

    Here is the code:
    Code:
    struct LEVELINFO
    {
    	int level[BLOCK_GRID_ROWS][BLOCK_GRID_COLUMNS];
    };
    
    int lev1[BLOCK_GRID_ROWS][BLOCK_GRID_COLUMNS] =
    	{
    		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    		{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    		{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    		{1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1},
    		{1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1},
    		{1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1},
    		{1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1},
    		{1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1},
    		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    		{1,1,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,1,1},
    	};
    
    LEVELINFO lev[5];
    
    void InitLevels()
    {
    	/*lev[0].level = lev1;
    	lev[1].level = lev1;
    	lev[2].level = lev1;
    	lev[3].level = lev1;
    	lev[4].level = lev1;
    	lev[5].level = lev1;*/
    	for(int i = 0; i<=5; i++)
    	{
    		lev[i].level = &lev1;
    	}
    }
    the error is:

    error C2106: '=' : left operand must be l-value

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    First, LEVELINFO.level is not a pointer, you can't assign it the address of lev1.

    Second, for(int i = 0; i<=5; i++) may crash your program, it'll try to access lev[5].level when lev only goes to 4 (0-4 = 5).

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  3. #3
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    PHP Code:
        for(int i 0i<=5i++)
        {
            
    lev[i].level lev1;//removed the &
        

    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by WebSnozz
    PHP Code:
        for(int i 0i<=5i++)
        {
            
    lev[i].level lev1;//removed the &
        

    Bzzzt. You just ran off the end of the array. Buffer overflows for everyone!
    Code:
    for (int i = 0; i < 5; ++i)
        lev[i].leve] = lev1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 01:02 PM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. what is wrong with this code?
    By Jennifer in forum Windows Programming
    Replies: 1
    Last Post: 01-06-2002, 08:50 AM

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