pass an array? (stock c++)

Started by
20 comments, last by cozzie 8 years, 6 months ago


while the vector version will be much safer, i believe the correct old school syntax should be:

void foo(int * a, int sizeX, int sizeY, int value)
{
a[sizeY*sizeof(int)+sizeX]=value;
}

No need for sizeof(int) at all. No need for sizeY at all. You need the stride (sizeX), and the indices of the elements you intend to access. You got this right in your second post: array[y*max_x+x]=value;


and you'll need to know the size of the array to resolve the address. so you end up passing array, x, y, max_x, and max_y for both one and two dimensional arrays!

You don't need sizeY for accessing elements -- You need it for bounds checking, if that's your bag -- but sizeX is sufficient for access.

throw table_exception("(? ???)? ? ???");

Advertisement
Before diving into the "how", what do you want to achieve?
Knowing this might help in finding the best solution, being a pointer, const ref to a struct/ class, std::vector or whatsoever.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement