0

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 ** ??

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by deceptikon
0

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.

Edited by Lucaci Andrew

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.