3

I'm developing a C program and I have a question about pointers and arrays.

I have the following array pointer:

GLuint *vboIds;

And the following function prototype:

void glGenBuffers(GLsizei n, GLuint *buffers);

The following statement is correct:

glGenBuffers(1, vboIds);

But I want to pass to glGenBuffers the second index of vboIds as second parameter for the function. I have put this:

glGenBuffers(1, &&vboIds[1]);

Is this correct?

Thanks.

3
  • 3
    "I have the following array: GLuint *vboIds;" Wrong, you have the following pointer. Remember, arrays != pointers, there is an extra layer of indirection with pointers using array notation. Commented Nov 29, 2010 at 8:07
  • @SiegeX: I have not worked with C for many years and I have forgotten almost everything. Commented Nov 29, 2010 at 8:14
  • 1
    Don't worry, this is the most common mistake that C programmers make, even those who have been at it for quite some time. To expand upon the "extra layer of indirection", assume you have declared int foo[10]; int *bar = foo;. When you do foo[2] = 1; the address of the array foo is a known constant and 2 * sizeof(int) is added to the address of foo to obtain the value. On the other hand, when you do bar[2] = 1; the first thing that has to happen is a fetch for the value of bar, deference that value to get to the array and then add 2 * sizeof(int) to get the value. Commented Nov 29, 2010 at 8:23

5 Answers 5

5
glGenBuffers(1, &(vboIds[1]));

or what Armen said,

glGenBuffers(1, vboIds + 1);
Sign up to request clarification or add additional context in comments.

3 Comments

That 1 you add to vboIds address would be valid with any vboIds' type. If I change its type to GLfloat, it would be valid?
It will always add a byte count equal to the size of the type vboIds is pointing at, whether that is GLUint or GLfloat.
or glGenBuffers(1,&(1[vboIds]) ); :P
3

Yes, it would be correct if you remove one ampersand.

You could also write glGenBuffers(1, vboIds + 1);.

1 Comment

Well, I made a mistake that I corrected in 10 seconds. In the meanwhile, someone gave me a downvote...
1
glGenBuffers(1, vboIds + 1);

Comments

0

No, the double & is incorrect, and will not compile. If you do want to use that syntax, it's:

glGenBuffers(1, &vboIds[1]);

This is completely equivalent to Armen's answer, which is the simplest way to do it.

Comments

0

You only need one address of operator (&)

glGenBuffers(1, &vboIds[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.