• Advertisement
Sign in to follow this  

trouble passing a pointer to an array of objects as a function variable(solved)

This topic is 4653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the correct syntax for passing a pointer to an array of objects to a function as an argument. here is what I have tried and it did not work.

//here is the function declaration
void SetupPieces(Piece* PlayPieces);

//and here is how I pass avarialbe to it

Piece thePieces[22];

SetupPieces(&thePieces);




here is compiler error i get
SetupPieces' : cannot convert parameter 1 from 'Piece (*__w64 )[7]' to 'Piece *'



[Edited by - donjonson on June 1, 2005 7:00:43 AM]

Share this post


Link to post
Share on other sites
Advertisement
an array and a pointer are essentially the same thing. when you declare an array (ie. int array[NUM_OBJECTS];) it is like saying "ok, give me a pointer to some memory address, and reserve NUM_OBJECTS * sizeof(int) memory for me to use." later, when you say "array[INDEX];" you are basically saying "hey, remember that memory i reserved earlier? well, i want to take a look at the address you gave me plus INDEX * sizeof(int)." so, some interesting things you can do:
*array + (INDEX * sizeof(int)) == array[INDEX];
*array == array[0];
array == &(array[0]);

anyway, so when you have an array, you have a pointer, and can treat it as such. there are some limitations that the complier imposes on you because it's a set size and all that, but for the most part, it's the same thing.

hope that helps you.


oh, sidenote: if you pass a pointer to a function, and expect to treat it as an array, make sure you have some way of knowing how big that array should be. if you are always going to pass the same size array, pass in an array[SIZE] instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement