2

How do you exactly use the operator ** (pointer to a pointer)?

I saw these operator used to declare a variable and is declared by a structure such as

struct name_of_struct **p_strutture;

What does it do exactly? What is the variable created?

How is this equivalent to *ptr[]?

What is its significance?

When is using a pointer to a pointer important in a program?

mc8
  • 315
  • 7
  • 21
  • There's no "pointer to pointer" operator in C. What you presented is a declaration. There's no operators in it at all. What you have there is just two `*` characters. Both are just characters, they are not operators. – AnT stands with Russia Jun 30 '13 at 16:18
  • 2
    @AndreyT sorry what i meant was pointer to pointer data type? – mc8 Jun 30 '13 at 16:23
  • 1
    I'm with @djf: you need to go back and reread a basic reference on c. Pointer are fundamental to doing any non-trival coding in c, and every resource of any quality explains them. – dmckee --- ex-moderator kitten Jun 30 '13 at 16:25

4 Answers4

4

A pointer to a pointer basically stores the address of another pointer. A variable has a memory location and a pointer variable is used to store this address. Similarly a pointer variable has a memory address and a pointer to a pointer stores such a memory address

  • 1
    can we have a pointer to a pointer to pointer? what is the significance of having a pointer to a pointer? – mc8 Jun 30 '13 at 16:20
  • 3
    *"can we have a pointer to a pointer to pointer?"* Yes, of course you can. But if you write code in which such a thing is explicit someone will likely come after you with an axe. – dmckee --- ex-moderator kitten Jun 30 '13 at 16:23
  • 1
    definitely, we can have any such levels of indirection and the meaning of such a thing can be induced from the above –  Jun 30 '13 at 16:24
  • 1
    thanks! when is pointer to a pointer usually used in a program? – mc8 Jun 30 '13 at 16:24
3

The type struct name_of_struct ** is a pointer to a pointer to struct name_of_struct see for example here:

// strutture is a struct name_of_struct object
struct name_of_struct strutture;

// p1_strutture is a struct name_of_struct * object
struct name_of_struct *p1_strutture = &strutture;

// p_strutture is a struct name_of_struct ** object
struct name_of_struct **p_strutture = &p1_strutture;

The & operator yields a pointer to an object.

ouah
  • 142,963
  • 15
  • 272
  • 331
3

If you declare:
char **myPointer;

you get a pointer pointing to a pointer.

Why would you do such a thing? 

If you want for example save some characters (text) you could use a two dimensional array, or you could use a pointer to a pointer.

With the two dimensional array the longest word would "assign" your array size (so a very short word would waste memory). With a pointer to a pointer you do not waste memory! So more elegant in this case would be an array of pointers and every pointer inside of that array points to an array of char.

myPointer = calloc(2, sizeof(char*));
char pointer1[] = "hello";
char pointer2[] = "world";

*myPointer = pointer1;
*(myPointer + 1) = pointer2;

The value of *myPointer would give you the address of pointer1.

The value of of *pointer1 would give you 'h'

This would be the same: *( (*myPointer)) and would also have value: 'h'

With: *( (*myPoniter) + 1) you get as value: 'e'

And *( *(myPointer + 1) ) would return: 'w'

noBillSide
  • 528
  • 5
  • 18
  • 1
    where did myPointer come from? – mc8 Jun 30 '13 at 16:58
  • 1
    myPointer is a pointer to pointer1? and *myPointer will give the value of pointer1 which is an address? – mc8 Jun 30 '13 at 17:03
  • did i understand correctly? – mc8 Jun 30 '13 at 17:05
  • @mc8 "where did myPointer come from?" -- It came from its author, in this case firfin. "myPointer is a pointer to pointer1?" -- myPointer is a pointer to a pointer to char. If it is set to &pointer1, then it points to pointer1. – Jim Balter Jun 30 '13 at 17:17
  • 2
    @JimBalter I asked " where did myPointer come from?" because it is not stated anywhere above what myPointer points to. However, the use pointer1, pointer and pointer3 was explicitly declared. I assumed that he forgot to state that myPointer is the pointer to an array of pointers given above. I clarified it and he confirmed it. I only made an assumption. – mc8 Jun 30 '13 at 17:39
  • @mc8 Apparently you meant to ask where the *value* of `myPointer` came from, but you didn't ask anything like that. – Jim Balter Jun 30 '13 at 17:42
  • @JimBalter It is implied. the author seems to have understood it. No I what my meant was "Where did myPointer come from. Its like saying a = c + 1 but c is not declared. – mc8 Jun 30 '13 at 17:47
  • @JimBalter why would I ask about the value of myPointer. That's utter nonsense. – mc8 Jun 30 '13 at 17:50
  • @mc8 Please stop being so rude. Your language shows confusion between variables and their values ... I say this as someone who has programmed in C for over 4 decades, whereas you are just learning. The value of `myPointer` is `&pointer1` ... that is precisely what you asked about, or meant to. The author did not answer your question "where did myPointer come from" so there's no way to know whether he understood it ... he responded to your second comment. – Jim Balter Jun 30 '13 at 17:56
  • *No I what my meant was "Where did myPointer come from.* -- As I said, it came from the author ... he provided a declaration of it. *Its like saying a = c + 1 but c is not declared.* -- That analogy doesn't make sense here. But we are now in an "extended discussion", which SO frowns upon, so I won't discuss it further. – Jim Balter Jun 30 '13 at 18:00
  • "fixed it" sorry for confusion! – noBillSide Jun 30 '13 at 18:09
  • 3
    @JimBalter I did not mean to be rude. It's just that I gotten the answer I needed. Then you really have to comment at everything. I understand your first comment that this question does not really meet the high standards of SO. I admit to that. But you don't have to insist on it and make me feel discourage because as I said I'm still a beginner, whereas a lot of other people here are kind enough to give me a hand. I tried to search for answers over the internet, I really did, but I saw SO as a website that gives a clear explanation to these answers. – mc8 Jun 30 '13 at 18:10
1

A pointer to a pointer stores the address of a pointer variable.A pointer variable is assigned a memory location just like some other variable . The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address.

But remember that representation of pointer values depends on the platform. They may be simple integral values (as in a flat memory model), or they may be structured values like a page number and an offset (for a segmented model), or they may be something else entirely.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49