1

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..?
}
3
  • 1
    Have a look at c-faq.com/aryptr/index.html Commented Nov 12, 2009 at 11:22
  • the link doesn't work.. apparently can't find the server. is it down or is it just me..? thx though Commented Nov 12, 2009 at 11:29
  • Then prefix the URL with cache: and type into google :) Commented Nov 12, 2009 at 11:35

4 Answers 4

3

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

void foo(int arr[5][5])
{
    cout << arr[2][2] << endl;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know I stated as fixed size but if I wanted to work with dynamic array/pointers, how would I go about accessing it?
3

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;

6 Comments

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.
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.
|
1

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.

Comments

1

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)

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.