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

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 .

u can have unlimited no. its not just 1 row. it is as many as u want i guess.

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.

You didn't mention where this is, I can think of two valid places:

GLfloat colours[][3] =
{
   { 1.0, 1.1, 1.2 },
   { 1.3, 1.4, 1.5 },
   <and on and on>
};

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:

void PrintColorArray( GLfloat colours[][3] ) // heck in the USA we spell differently
{
    for (int i = 0; i < ?????; i++) // what do we fill in here?
        printf( "Color %d is %f,%f%f\n", i, colours[i][0], colours[i][1], colours[i][2] );
}

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).

This article has been dead for over six months. Start a new discussion instead.