Thread: Whats wrong with my array?

  1. #1

    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
    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 08: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
    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
    But it is still bringing up the same error... Saying that the braces are a syntax error.

  8. #8
    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
    Why do you have main() taking arguments? Anyways it is still bringing up the same error.

  10. #10
    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
    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
    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
    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"}};

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, 01:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM