Whats wrong with my array?

This is a discussion on Whats wrong with my array? within the C++ Programming forums, part of the General Programming Boards category; Code: char board[3][3] = { "0.0", "0.1", "0.2", "1.0", "1.1", "1.2", "2.0", "2.1", "2.2" }; It seems like it is ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Whats wrong with my array?

    Code:
    char board[3][3] = { "0.0", "0.1", "0.2", "1.0", "1.1", "1.2", "2.0", "2.1", "2.2" };
    It seems like it is fine to me. But I am getting 102 errors in my code, most just complaining that I need an array to use it like that. The array brings up these errors-
    board.h(16) : error C2059: syntax error : '{'
    (16) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    EDIT: array's indention was skrewed so I just put it that way.

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    for one thing it should be char board[3][4] because you're storing a 3 element char + the null terminator.

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Wha? I thought it should be [3][3] because I only assign 9 elements in the array...
    0 1 2
    0 1 2
    0 1 2
    ???

    What exactly is the "null terminator"?

    EDIT: NVM, isn't it \0?
    Last edited by Munkey01; 02-27-2003 at 07:41 PM.

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    A null terminator is '\0' which is added to the end of a char array. Also, you aren't declaring just 9 elements. If you want 9 char arrays that each hold a 4 element char arrays then you'll want this:

    char board[9][4] = {stuff........etc};

    This is what's happening: You are wanting to store 3 chars into an array (example: "0.0" is 3 because you have 2 "0"s and the period seperating them). You have to give enough room for the null terminator, so you need a 4 element char array.

    Here's what it'll look like:
    Code:
    board[0]="0.0";
    board[1]="0.1";
    board[2]="0.2";
    board[3]="1.0";
    board[4]="1.1";
    board[5]="1.2";
    board[6]="2.0";
    board[7]="2.1";
    board[8]="2.2";
    
    //so if you break it down, they'd look like this:
    board[0][0]='0';
    board[0][1]='.';
    board[0][2]='0';
    board[0][3]='\0';
    
    board[1][0]='0';
    board[1][1]='.';
    board[1][2]='1';
    board[1][3]='\0';
    
    //etc etc

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    But why can't I declare my array like it is?

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Munkey01
    But why can't I declare my array like it is?
    You can declare it like it is, but you can't dump that much data into the way it's declared. Just change it to:

    Code:
    char board[9][4] = { "0.0", "0.1", "0.2", "1.0", "1.1", "1.2", "2.0", "2.1", "2.2" };
    If you insist on declaring it the way you have then you'll be restricted to what you can initialize it to. Like this:

    Code:
    char board[3][3] = {"0.", "1.", "2."};
    The reason why is because of the basic semantics of the language. If you want to store X amount of information in an array then you need to declare the array to size X.

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    But it is still bringing up the same error... Saying that the braces are a syntax error.

  8. #8
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    You have 3 rows and 3 columns of 4 element arrays

    You need:

    char board[3][3][4] // init list;

    or if you will not modify the contents you can use:

    char *board[3][3] // init list;

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
    	char board[3][3][4] = {{"0.0", "0.1", "0.2"}, {"1.0", "1.1", "1.2"}, {"2.0", "2.1", "2.2" }};
    	for (int i=0; i<3; ++i)
    	{
    		for (int j=0; j<3; ++j)
    			std::cout << board[i][j] << " ";
    		std::cout << std::endl;
    	}
    	return 0;
    }

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Why do you have main() taking arguments? Anyways it is still bringing up the same error.

  10. #10
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    The declaration of main was inserted by my IDE. It is a legal declaration, but the args are not required by this little program.

    Try pasting my code into a file and compile it. There is no way you should still get that same error.

  11. #11
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Maybe if I change my code around to deal with an integer array instead maybe all of this confusing can be settled, hmmmm.

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Try removing or replacing double qoutes to single or seperating the variables with just commas " , ". and jdinger may be right.
    You need [9][4].

    You are inputting as strings and may need the null terminator.

  13. #13
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Keep getting problems. I even tried using an integer array instead of a character but I still come up with the same error.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a snippet of your latest attempt that's giving you trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I am not positive it is even declared correctly, but VC++6 keeps complaining about it.

    Code:
    int board[3][3] = {{"00", "01", "02"}, {"10", "11", "12"}, {"20", "21", "22"}};

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dont know what im doing wrong... array
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2009, 12:23 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

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