We help IT Professionals succeed at work.

# double array

on
Medium Priority
270 Views
Hi

I have problems passing a double array to a function.
I wrote two examples.
One involves a simple array. It works.
The second involves a double array.Id doesn.t.

First example

#include<stdio.h>
#include<iostream.h>

void printArray(int*);

int theArray;
int anotherArray;

void printArray(int* givenArray)
{
for(int i = 0; i < 8; i++)
{
cout << givenArray[i] << endl;
}
}

int main()
{

for(int i = 0; i < 8; i++)
{
theArray[i] = i;
anotherArray[i] = 10 + i;
}

printArray(theArray);
cout << endl;
printArray(anotherArray);

return 0;
}

Second example:
#include<iostream.h>

void printArray(int**);

int theArray;
int anotherArray;

void printArray(int** givenArray)
{
for(int i = 0; i < 8; i++)

for(int j = 0; j < 8; j++)

cout << givenArray[j][i] << endl;

}

int main()
{
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)

{
theArray[j][i] = i;
anotherArray[j][i] = j;
}
}

printArray(theArray);
cout << endl;
printArray(anotherArray);

return 0;
}

The error message that I get is"can not convert int*
to **".

I understand that in the case of a single array,
theArray is equivalent to *theArray;

A pointer to the first member of the array.
I think that in case of a double array, we deal with a pointer to the first element of the array wich is an array itself that means is a pointer to the first element of the array.
So in case of a double array we deal with a pointer to a pointer.
When I passed the simple array to the function it was ok, because the function was defined as receiving a pointer to an integer and theArray was a pointer to an integer.

The second time, the function is defined as receiving a pointer to a pointer to an integer. theArray is a pointer to a pointer to a integer, still the program complains.

Thank you.
Comment
Watch Question

## View Solution Only

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.