Hi,

I have a doubt. Suppose we define a pointer or an array of pointers, and pass it to a function, as in like :-

 char *p;
 func(p);

 /*or this way*/

char *p[10];
 /*and then pass this array somewhere to a fucntion*/

 func(p);

Here, in the call to the function, what are we passing as an arguement, is p is here a char pointer or a char ** ??

Recommended Answers

All 2 Replies

Here p is a char pointer, like char *.
Take for example this:

#include <stdio.h>

char* f(char *p){
    printf("%s\n", p);
    return (p);
}

int main(){
    char *p={"Huga Huga"};
    char *l;
    l=f(p);
    printf("%s", l);
    return (0);
}

Here I have passed the char p to the function f. As you can see, the function f than is of type char *, and it should return also a char *. Let's say we put in the return value, the actual pointer that we sent, p.
So, in the main, we put another char pointer, char *l in which would be stored the return value of the function f.
Accordingly to that, there shouldn't be any problems, and there aren't.
So, you saw that what I sent as a parameter, the p, then it's returnet as a char
and it's ok.
If you have further questions, just post them here.

Here, in the call to the function, what are we passing as an arguement, is p is here a char pointer or a char ** ??

In the first call the argument is char*. In the second call it's char**. An important difference to note is with multidimensional arrays. Consider the following code:

char a[M][N];
func(a);

Due to the law of decaying pointers, one might mistakenly think that the argument type is char**, but in reality only the first dimension of the array follows the law, which means the actual type is char(*)[N] (a pointer to an array of N).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.