Thread: Newby array problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    19

    Newby array problem

    Hi,
    I have never done anything with multi dimensinal arrays before, and got stuck with a simple code, what's supposed to fill array "Table" with zeros. I keep getting error: error C2059: syntax error : '['
    I'd be greateful, if sb told me, what's wroing with it.
    Code:
    int Table[12][12];
    for ( int a = 0; a < 12; a++ ) {
    	for ( int b = 0; b < 12; b++ ) {
    		Table[a][b] = [0][0];
    	}
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you're not assigning right, just change the [0][0] to 0.

    Code:
    int Table[12][12];
    for ( int a = 0; a < 12; a++ ) {
    	for ( int b = 0; b < 12; b++ ) {
    		[b]Table[a] = 0; //nothing special here, just a regular assignment
    	}
    }
    another shortcut would be to do this:
    Code:
    int Table[12][12]={0};
    That initializes the entire array of arrays to zero.
    Last edited by major_small; 11-28-2005 at 02:02 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is an easier way to initialize an array:
    Code:
    int Table[12][12] = {0};
    When you use an initializer list, which is what is on the right hand side, and you don't specify all the values for each index position, then the positions you don't specify will be initialized with 0. So, all you have to do is specify 0 for the first element in the array, and since the rest of the elements aren't specified, they will be initialized with 0.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    thanks for reading my post
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx for all the answers, but I have one more question: Is there something like .length value in Java? I tryed sizeof() But it just repeats the action 12x12 times, when used in loop.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    not really, if you're using C-style strings, you could use strlen(char*) from the <cstring> header.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In C++, you would generally use the standard vector class for this purpose. That class has a length() method. The syntax might look odd at first, but vectors are easier and safer in the long run than regular C style arrays. This is the equivalent to a 12x12 int array initialized to 0:
    Code:
    std::vector<std::vector<int> > Table(12, std::vector<int>(12));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM