Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ** in C++?

Tags:

c++

pointers

I've seen some code, as well as some errors generated from my compiler that have a '**' token before the variable (eg **variablename unreferenced-- or something, I can't recall exactly offhand). I'm fairly certain this is related to pointers, if I had to guess it looks like it's trying to dereference twice. '**' is fairly ungoogleable. Can someone point me to a good website/documentation or would someone care to explain it here?

Thanks.

Great responses. If I can add, what would be some situations where it is useful to have a pointer to a pointer? Shouldn't you just be using the original pointer instead of creating yet another pointer to the original pointer?

like image 443
agscala Avatar asked Mar 13 '09 23:03

agscala


People also ask

What is double pointer in C?

A pointer is used to store the address of variables. So, when we define a pointer to pointer, the first pointer is used to store the address of the second pointer. Thus it is known as double pointers.

What is the use of * in C?

The text inside the /* and */ tags will be considered as comments and will not be executed or compiled. This is to provide the coder with a clear knowledge of the code and its application or use. The output is printed to the console screen using this C command. This function is being used to wait for the user's input.

Why do we need double pointer in C?

Double pointers can also be used when we want to alter or change the value of the pointer. In general double pointers are used if we want to store or reserve the memory allocation or assignment even outside of a function call we can do it using double pointer by just passing these functions with ** arg.

What is the use of * p in C?

It's purpose is to print a pointer value in an implementation defined format. The corresponding argument must be a void * value. And %p is used to printing the address of a pointer the addresses are depending by our system bit.


1 Answers

** is not actually only pointer to pointer (as in declaration), but is also the dereference of a dereference (in a statement).

It is used often in C which does not have the & notation for references, e.g. to update a return value which is a pointer type:

int alloc_foo(struct foo **foo_ret) {     *foo_ret = malloc(sizeof(struct foo));     return 1; /* to indicate success; return value in foo_ret */ } 
like image 114
Antti Huima Avatar answered Oct 09 '22 17:10

Antti Huima