简体   繁体   中英

Difference between passing “pointer to pointer” and “address of pointer” to a function

I have a function that takes a char ** ; it uses it internally for maintaining context between successive calls.

What is the difference between defining char **x and passing x to it, and defining char *x and passing &x ?


For context: I tried to implement the example at strtok man page on my own before reading the source code. And I got segfaults. Then after attempts I looked at the source.

The problem was that I defined char **x and passed x as the **saveptr argument, but changing the definition to char *x and the passing to &x solved the problem.

What is the problem exactly?

The first version, with char **x; and passing x , creates and uses an uninitialised pointer to pointer to char.
The second version, with char * x; and passing &x , creates an uninitialised pointer to char, but passes a value which is a valid address of a pointer to char, and is a defined value (ie like an initialised pointer to uninitialised pointer to char).

Basically with first version you ask to write at a "random" place in memory (almost sure way to get a segfault); with second, you ask to write into an existing pointer variable.

When you define eg

char **x;

Then you simply define a pointer. It doesn't matter that it's a pointer to another pointer, the important part is that it's just a pointer. And as such, with the definition shown above, it's not initialized. It doesn't point anywhere special. Dereferencing it, like you most likely do in the function, leads to undefined behavior .

On the other hand, with a definition like

char *x;

and using &x to get a pointer to it, the pointer returned by &x points somewhere valid, to the variable x . Dereferencing &x will allow the function to access and modify x itself.

Using the address-of operator in this way is a way to emulate pass by reference , which C doesn't have.

Because all function arguments are passed by value in C, if you define char **x and pass x , it won't be modified by the function. If you define char *x and pass &x , it can be modified (change its pointing address).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM