array declaration explanation needed

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 11
Reputation: mattcplusplus is an unknown quantity at this point 
Solved Threads: 0
mattcplusplus mattcplusplus is offline Offline
Newbie Poster

array declaration explanation needed

 
0
  #1
Nov 21st, 2004
Hi

i need an explanation of the following array declaration:

GLfloat colours[][3]

Why are the square brackets empty, does it imply a default value? so it has say 1 row and three columns?

Thanxfor your help in advance it is much appreciated
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: array declaration explanation needed

 
0
  #2
Nov 21st, 2004
In c /c++ there is facility of not giving the dimensions of row in case of 2 dimensional array.
for eg u can also write char str[]={"Welcome to c world"}; . Its perfectly valid statement .
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 14
Reputation: Tresa is an unknown quantity at this point 
Solved Threads: 0
Tresa Tresa is offline Offline
Newbie Poster

Re: array declaration explanation needed

 
0
  #3
Nov 21st, 2004
u can have unlimited no. its not just 1 row. it is as many as u want i guess.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: array declaration explanation needed

 
0
  #4
Nov 21st, 2004
Oh obviously u can have unlimited number there
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: array declaration explanation needed

 
0
  #5
Nov 21st, 2004
no you can't.
It just means the number is undefined. You're declaring an array of 3 arrays. The number of elements of each of those 3 arrays is unknown at this time but will have to be fixed at some point.
It doesn't even have to be the same number of elements for each I think (not 100% certain here, could be I'm confused with Java where such is the case).

Of course unlimited numbers are impossible as the index must be an integer type and therefore fit into a 32 bit (or 64 bit depending on compiler/platform) word. It's also limited by the amount of installed memory in the client machine.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: array declaration explanation needed

 
0
  #6
Nov 21st, 2004
You didn't mention where this is, I can think of two valid places:
  1. GLfloat colours[][3] =
  2. {
  3. { 1.0, 1.1, 1.2 },
  4. { 1.3, 1.4, 1.5 },
  5. <and on and on>
  6. };
In this case, the declaration fixes the size based on how many initializers you give it. So it IS a fixed number, but you didn't have to tell the declaration syntax how many.

The other case is like this:
  1. void PrintColorArray( GLfloat colours[][3] ) // heck in the USA we spell differently
  2. {
  3. for (int i = 0; i < ?????; i++) // what do we fill in here?
  4. printf( "Color %d is %f,%f%f\n", i, colours[i][0], colours[i][1], colours[i][2] );
  5. }
In this case you have to somehow know at run time how big the array is; for example as a parameter to the function.

In this second case, you could pass many different arrays with different sizes to this routine (as long as they were triplets of GLfloats).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC