Thread: Add values to muldimentional array after declaration

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    17

    Add values to muldimentional array after declaration

    I'm trying to make a big 3 dimentional array with a variable as value. Because of this, i cannot initialise it during declaration. I can't, however, find a way to insert the values into the array afterwars without inserting it one by one.
    This is my code:
    Code:
    GLfloat faces[6][4][3];
    
    faces =
    {
    	{	//face 1
    		{ 0,0,0 },
    		{ 0,size,0 },
    		{ size,size,0 },
    		{ size,0,0 }
    	},
    	{	//face 2
    		{ size,0,0 },
    		{ size,size,0 },
    		{ size,size,size },
    		{ size,0,size }
    	},
    	{	//face 3
    		{ size,0,size },
    		{ 0,0,size },
    		{ 0,size,size },
    		{ size,size,size }
    	},
    	{	//face 4
    		{ 0,0,0 },
    		{ 0,0,size },
    		{ 0,size,size },
    		{ 0,size,0 }
    	},
    	{	//face 5
    		{ 0,size,0 },
    		{ size,size,0 },
    		{ size,size,size },
    		{ 0,size,size }
    	},
    	{	//face 6
    		{ 0,0,0 },
    		{ size,0,0 },
    		{ size,0,size },
    		{ 0,0,size }
    	}
    };
    It says there is a syntax error at "faces=".

    I've tried it with a very simple version like
    Code:
    int a[2];
    a= {1,2};
    but that won't wok either. If I use
    Code:
    int a[2]={1,2};
    then there's no problem but because i use the variable "face" in the bigger array, i can't initialise it like that.

    How am I supposed to do this? I've tried an impossible number of combination and nothing seems to work but this has to be possible...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you're using C99, on the other hand, Bayint provided a good solution.
    If you're using C90, then you can create a temporary array, initialize it, and copy it in a similar manner to Bayint's solution.
    Last edited by Elysia; 10-23-2010 at 03:43 PM.
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you are using C99, you can use compound literal.
    Code:
    memcpy( faces,
    ( int [6][4][3]) 
    {
    	{	//face 1
    		{ 0,0,0 },
    		{ 0,size,0 },
    		{ size,size,0 },
    		{ size,0,0 }
    	},
    	{	//face 2
    		{ size,0,0 },
    		{ size,size,0 },
    		{ size,size,size },
    		{ size,0,size }
    	},
    	{	//face 3
    		{ size,0,size },
    		{ 0,0,size },
    		{ 0,size,size },
    		{ size,size,size }
    	},
    	{	//face 4
    		{ 0,0,0 },
    		{ 0,0,size },
    		{ 0,size,size },
    		{ 0,size,0 }
    	},
    	{	//face 5
    		{ 0,size,0 },
    		{ size,size,0 },
    		{ size,size,size },
    		{ 0,size,size }
    	},
    	{	//face 6
    		{ 0,0,0 },
    		{ size,0,0 },
    		{ size,0,size },
    		{ 0,0,size }
    	}
    },
    
    sizeof(faces) );
    Btw, make sure that you really need 3 dimensional array. Maybe you should be using struct?
    Last edited by Bayint Naung; 10-23-2010 at 03:44 PM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    17
    Thanks Bayint, that works. It's too bad you have to use tricks like memcpy in c instead of something a bit more straightforward. I also didn't know you could include an array layout in a typecast.
    I'm using it to define the vertices of a cube to draw in OpenGL. I don't think a struct would really work (I might be looking at it in the wrong way)

    Elysia, I see one problem with your solution. I will be adjusting size to vary the size of the cube. Creating a temp array everytime I change the size will leave that memory used and if I try to declare another int with the same name, the compiler refuses to do so because it was already declared under that name. I tried using free() but that only works if the int was created with malloc which then just brings me back to my original problem. Do you have a way around that?

    I'm using gcc from the MinGW package for windows and as it will compile, I guess I'm using c99 but how can I check which version the system is using?
    Last edited by njitram2000; 10-24-2010 at 02:19 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by njitram2000 View Post
    Thanks Bayint, that works. It's too bad you have to use tricks like memcpy in c instead of something a bit more straightforward. I also didn't know you could include an array layout in a typecast.
    I'm using it to define the vertices of a cube to draw in OpenGL. I don't think a struct would really work (I might be looking at it in the wrong way)
    I'm afraid that is how C is. A minimal language including only the most necessary features.

    I'm using gcc from the MinGW package for windows and as it will compile, I guess I'm using c99 but how can I check which version the system is using?
    MinGW is fully C99 compatible. There should be a C99 switch. You should enable pedantic and ansi or whatever these arguments are called in order to avoid bad practices and be fully standards compliant.
    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. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  4. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM