Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C++
main()
{

int a[100][100],r;
.
.
.
..//here i want to call this function please tell me how can i do this
..
...

}
void traverse(int **a,int r)
{…
..
..
...}
Posted
Updated 22-Aug-14 9:27am
v2
Comments
Sergey Alexandrovich Kryukov 22-Aug-14 15:55pm    
What's wrong with just reading at least the most elementary language manual, before writing any code?
But it's ok, I answered the question, see Solution 2.
—SA

Move traverse definition to a location in your file before main.

In some other, more complex cases, with mutual dependency between two functions (no, this is not your problem, you "problem" is not a problem, really), you can put forward declaration before the calling function, and below, add the definition. (Declaration of a function is a function without its main body block, without "{...}"; it is used to provide just the signature for a caller, but not implementation; the same language feature is used in header files.)

—SA
 
Share this answer
 
v2
As opposed to what solution 1 says, the correct way of calling the function is:
C++
int* pa = a[0];
traverse (&pa, r);

Here is the background: In C++ an array name delivers a pointer to the first element. Thus, in the following example, the name b is equivalent to a pointer to the first element of b and hence delivers an int*.
C++
int b[50];
int* pb = b;

It would be an error to write
C++
int* pb = &b; // wrong

and the compiler will probably complain about that. Now, for a two dimensional array this nice shorthand doesn't work. If we have
C++
int a[100][100];
int* pa = a; // does not compile

because a stands for the address of the first row, i.e. a pointer to an array of "int [100]". If we dereference that pointer, however, we get again a pointer to the first cell:
C++
int a[100][100];
int* pa = *a;

The next thing you notice is, that we never get an int**, i.e. a pointer to a pointer to int, but always a pointer to int. That is because C++ just allocates space for the array cells, but not for row pointers. If we really need a pointer to a pointer, we have to declare this pointer separately, as we did above with pa.

By taking the address of this pointer we can at least satisfy the requirements of our function call:
C++
traverse (&pa, r);

BUT: This is probably not what the author of function traverse expected. If someone declares a parameter int**, he probably expects that we pass an array of pointers to arrays of int. And that is something, which we need to construct first:
C++
int a[100][100];

int* rows[100];
for (int i = 0; i < 100; ++i)
    rows[i] = a[i];
traverse (rows, r);

The other alternative is that the author of function traverse did not really know how to handle two-dimensional array and he meant to declare traverse actually as:
C++
traverse (int* a, int rows, int cols, int r);

Note that the parameters rows and cols are necessary, because just from parameter a the function will not be able to figure out the dimensions of the array.

And as Sergey has pointed out in his comment, you should do an advance declaration of the function if the call appears earlier in your source code than the functions declaration, for example:
C++
void traverse (int** a, int r);

void main ()
{
    int a[100][100], r;

    ...

    int* rows[100];
    for (int i = 0; i < 100; ++i)
        rows[i] = a[i];
    traverse (&pa, r);

    ...
}

void traverse (int** a, int r)
{
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900