944,192 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5398
  • C RSS
Nov 21st, 2004
0

array declaration explanation needed

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mattcplusplus is offline Offline
11 posts
since Oct 2004
Nov 21st, 2004
0

Re: array declaration explanation needed

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 .
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004
Nov 21st, 2004
0

Re: array declaration explanation needed

u can have unlimited no. its not just 1 row. it is as many as u want i guess.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tresa is offline Offline
14 posts
since Nov 2004
Nov 21st, 2004
0

Re: array declaration explanation needed

Oh obviously u can have unlimited number there
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004
Nov 21st, 2004
0

Re: array declaration explanation needed

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 21st, 2004
0

Re: array declaration explanation needed

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).
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Solution Of Simultaneous Equation
Next Thread in C Forum Timeline: Weird segfault involving popen





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC