Thread: 3D array

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    3D array

    Anyone that's seen one of my previous posts over the past few days , most of that stuff has now been scrapped as I thought we needed to have an uninitialized array, but it turns out we don't.

    anyway, so now I'm trying to declare this array but its throwing out an error that seems weird to me but I'm sure its simple. here's the array that's causing the error;

    Code:
    	char array1[3][7][7] =
    	{
    	{
    	{'0','0','0','1','0','0','0'},
    	{'0','0','1','1','0','0','0'},
    	{'0','0','0','1','0','0','0'},
    	{'0','0','0','1','0','0','0'},
    	{'0','0','0','1','0','0','0'},
    	{'0','0','1','1','1','0','0'}
    	},
    	{
    	{'0','0','2','2','2','0','0'},
    	{'0','2','0','0','0','2','0'},
    	{'0','0','0','0','2','0','0'},
    	{'0','0','0','2','0','0','0'},
    	{'0','0','2','0','0','0','0'},
    	{'0','2','0','0','0','0','0'},
    	{'0','2','2','2','2','2','0'}
    	},
    	{
    	{'0','3','3','3','3','0','0'},
    	{'0','0','0','0','0','3','0'},
    	{'0','0','0','0','0','3','0'},
    	{'0','3','3','3','3','0','0'},
    	{'0','0','0','0','0','3','0'},
    	{'0','0','0','0','0','3','0'},
    	{'0','3','3','3','3','0','0'};
    
    	//populate(array1);
    
    	display(array1);
    
    	return 0;
    and this is the error

    ICA2.cpp:43: error: expected `}' before ‘;’ token
    ICA2.cpp:43: error: expected `}' before ‘;’ token
    (it shows twice)

    thing is, there already is a ; before the } on line 43. is there meant to be one further up?

    thanks in advance

    ES

    P.S I realise the populate function call is a comment, I've left that function in from a previous program in case I need it later

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You're missing two } at the end. It's pretty easy to spot considering the indentation.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Is this syntax possible in C++? I thought you could only use two brackets. One opening one and one closing one. With only two it works for sure. You would just have for example

    Code:
    char array[2][2] = { '0', '1',
                                    '1', '1'};
    The newline is optional, you could put the four values in one line if you wanted, but it makes more sense like above. The result is the same in any case.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    edit; added some variety because I'm bored. :]

    You must not understand how brackets work if you can't figure that out. Study what I've written below very carefully, then look at your source, you'll see where you missed the brackets.

    This is a 1D array;
    Code:
    int array[1] = {1};
    int array[3] = {1,2,3};
    This is a 2D array;
    Code:
    int array[2][2] = { {1,2}, {3,4} };
    int array[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
    This is a 3D array;
    Code:
    int array[3][3][3] = { { {1,1,1}, {2,2,2}, {3,3,3} }, { {4,4,4}, {5,5,5}, {6,6,6} }, { {7,7,7}, {8,8,8}, {9,9,9} } };
    int array[2][3][3] = { { {1,1,1}, {2,2,2}, {3,3,3} }, { {4,4,4}, {5,5,5}, {6,6,6} } };
    int array[2][2][3] = { { {1,1,1}, {2,2,2} }, { {3,3,3}, {4,4,4} } };
    int array[1][2][3] = { { {1,1,1}, {2,2,2} } };
    I am explaining this too death, I'm going to get back to work.
    Last edited by since; 12-15-2009 at 11:15 AM.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    *face palms* of course! I see it now, I knew it would be something simple/stupid

    you'll have to forgive me, I'm not used to spotting these little errors yet, but I'm still learning.

    Anyway, Thanks for the help

    ES

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Take care to indent properly. If you wish to continue over several rows, put each dimension's { on an extra tab or some spaces of indentation. Just like you would do for blocks. It makes it easy to spot mistakes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Array of Bool
    By cybernike in forum C++ Programming
    Replies: 37
    Last Post: 06-28-2007, 11:17 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. 3D array
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 04:00 PM
  4. 3D Array
    By Alextrons in forum Windows Programming
    Replies: 4
    Last Post: 01-11-2002, 01:39 AM
  5. Dynamic 3D array allocation
    By Vulcan in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 02:51 AM