Pixels, videoGrabbers, pointers and references

Hi! I am trying to solve this for a better understaing of pointers, references and how ofPixels and ofVideoGrabbers manage their contents
Let’s say I have 3 different videoGrabbers, vidGrab0, vidGrab1 and vidGrab2.

vidGrab0.getPixels() returns a pointer to the array where the pixels are stored,
if I want to Access directly to the pixels, I have to use the reference:

// code 1)
ofPixels & pixels = vidGrab0.getPixels();

That let me acces to any pixel in the pixels array. pixels is an array of ofPixels

I want to store those pointers in an array, that is -if I am right- a pointer to the first pixel of each videoGrabber.
It is my understanding that

// code 2)
ofPixels * pixelsArray[3] // this is my array of pointers to ofPixels
pixelsArray[0] = &videoGrab0.getPixels() // I store a reference to the pointer

That compiles :smile:

Later, I will use an auxiliar ofPixel object to mess around with those pixels. And here is where the problem is.
How do I get an array, like in code 1) ?
I guess that the way to do it should be

ofPixels & pixels = pixelsArray[0]; // since I stored a reference of the pointer in the array

But does simply doesn’t work. Can somebody throw some light? Should I approach this using an array of arrays? This isin instead of pixelsArray[3], use pixelsArray[3][] and store the pixels directly?
Thanks a lot!

what you are storing are pointers not references so you should be doing:

ofPixels * pixels = pixelsArray[0];

and then using it like: pixels->getWidth()

Hi Arturo,
First of all, thanks for answering these kind of C++ questions.
You are right, what I am storing has to be a pointer, since pixelsArray is define like an arrays of pointer. But if I am not wrong, that’s a pointer to a reference, right?

I am afraid there is something I am not understanding correctly. Using code 1) allows me to access pixels like in an array, pixles[i]
But if I use what you are suggesting, there is no way to use pixels->[i], correct?

I don’t want to take much of your time with this, I am afraid I am just trying to make things too complicated…

Edit: It worked the other way around:

ofPixels pixels = *pixelsArray[0];

Now, I can access to pixels[i]

Thanks for your help! - Now I have to figure out why it worked…

a pointer to a reference doesn’t make really sense, when you dereference (take a pointer) of a reference you are just taking a pointer (the memory address) of the original object,

with the pointer to ofPixels, you can still do:


but to make the syntax easier you can just do:

ofPixels & pixels = *pixelsArray[0];

OK, so just to make it clear:

ofPixels & pixelsRef = *pixelsArray[0] //creates a reference to the orignal pixels
ofPixels pixelsCopy = *pixelsArray[0] //creates a copy of the pixels originally referenced.

Sorry, I know that is not inituitive nor meaningful but at least I get an understanding on what I am doing…

yes that’s it