views:

259

answers:

4

A simple question about accessing array information through pointers that I can't seem to figure out. I'm passing a bunch of multi-dimentional arrays into a function. Now they are not dynamic but even if static, I have to pass them as pointers right? (If I'm wrong, please do correct me)

So once I do pass them into a function, how do I access it..?

int main()
{
    int anArray[5][5] = // member intializations
    foo(*anArray);
}
void foo(int * anArray) //or should that be int ** anArray ??
{
    cout << anArray[2][2] << endl; // how should i address this..?
}

If someone could explain that would be greatly appreciated..

Dean

A: 

You can access a pointer just like you can access an array. So your "foo" function is correct. The main issue I see with your code is this line

foo(*anArray);

change it to

foo(anArray);

or if you want to be really explicit

foo(&anArray[0][0])

Now err... umm... a few pointers. Since a pointer is an array is a pointer, you can really shoot yourself in the foot. For example in foo, you know that the memory being passed in is a pointer, but you probably also want to communicate its size for appropriate checking. Many a buffer overflow has resulted from assumptions about incorrect array size. This is called a buffer overrun, and can result in you inadvertently overwriting memory in other parts of your program's memory (well hopefully your program's) causing very strange behavior. For example a stack variable in your program may suddenly take on a value it was not assigned.

If you have access to std::vector or boost::array, I'd reccomend those to avoid any issues with buffer overruns.

Doug T.
+2  A: 

If your arrays are of fixed size, you can do it this way:

void foo(int arr[5][5])
{
    cout << arr[2][2] << endl;
}
Ferruccio
I know I stated as fixed size but if I wanted to work with dynamic array/pointers, how would I go about accessing it?
http://www.functionx.com/cpp/Lesson14.htmhopefully that is correct..
+2  A: 

An array is a series of objects laid out consecutively in memory.

int blah[5][10];

is an array of 5 [array of 10 [int]s]s.

For instance, blah[0] will give you the 1st array of 10 ints, blah[1] will give you the second, etc.

To be able to calculate memory offsets, C needs to know exactly how many elements are in each "subarray".

So you could do this

void foo(int anArray[][10]) {
    cout << anArray[2][2] << endl;
}

or this

void foo(int anArray[5][10]) {
    cout << anArray[2][2] << endl;
}

but you cannot do [][].

This is quite different from

void foo(int *anArray[10]) { //array of 10 pointers

or

void foo(int **anArray) { //pointer to pointer (could be a pointer to an array of pointers to more arrays)

If you don't know the dimensions of the array in advance, you have to use a more complex data structure. Maybe pass the width/height of the array to the function (or use a struct containing that information) and resolve the 2 dimensions onto a 1-dimensional array, like so:

int *arr = malloc(width*height * sizeof(int));
...
cout << arr[x + y * width] << endl;
Artelius
a question.. in case i pass static array, shouldn't foo(anArray) in int main suffice? because i'm getting errors, cannot convert 'int' to 'int*'
since anArray's size is already known, I just pass the name of array to foo... thx for the help, btw.
`foo(anArray)` shouldn't give you that error, because `anArray` is not an `int`.
Artelius
well this is the actual code, in int main, classify(coords, centroids, classified); and void classify(double coords[8][2], double centroids[3][2], int classified[8]). the error i get is error: cannot convert 'double' to 'double*' for argument '1' to 'void classify(double*, double*, int*)' which kind of throws me off because it doesn't complain about the second argument which is a same 2d array with same type, just of different size. sorry for the messy code
You haven't posted enough of your code. Specifically, the definition of `coords` inside `main`. Edit your question and put it there.
Artelius
i figured out what was wrong. something silly that i didn't fix while trying out things. thank you very much!!
You're welcome.
Artelius
A: 

in C++ the array is actually a pointer to the first element.
when passing *anArray where anArray is anArray[5][5], you are passing the content of cell anArray[0] which is a pointer to a integer array,

so the foo function should be receive a pointer: void foo(int* intanArray)

Alon