5

If there are pointers in C (char *names[]) and pointers to pointers (char **cur_name = names); Can there be a pointer to a pointer to a pointer?

Or is a pointer to a pointer to a pointer just a linked list? Maybe it's a stupid question, but I'd like to know the answer.

  • Shorten the title. And yes,3 levels of pointers are supported. In fact,[many levels are possible](http://www.stackoverflow.com/questions/10087113/how-many-levels-of-pointers-can-we-have) – Spikatrix Dec 17 '14 at 17:12
  • This question has already been asked! Check [this](http://stackoverflow.com/questions/22002248/double-and-triple-pointers-in-c) thread! – delirium Dec 17 '14 at 17:13
  • 2
    A pointer and a linked list are two quite different things. – Mike Sherrill 'Cat Recall' Dec 17 '14 at 17:13
  • In general, if the code has to de-reference beyond two levels, it is time to seriously consider a re-design of the code. – user3629249 Dec 17 '14 at 17:50

6 Answers6

5

Yes, you can have an arbitrary number of levels of pointers.

int x = 5;
int *a = &x;
int **b = &a;
int ***c = &b;

printf("%d %d %d %d\n", x, *a, **b, ***c);

A pointer to a pointer to a pointer is not a linked list. A linked list is a structure type that contains a pointer to its own type:

struct list
{
   int data;
   struct list *next;
};

So that you can chain them together in a list:

struct list three = { 3, NULL };
struct list two = { 2, &three };
struct list one = { 1, &two };
struct list head = { 0, &one };

And iterate over them:

for (struct list *node = &head; node->next; node = node->next)
{
    printf("%d\n", node->data);
}
Carl Norum
  • 201,810
  • 27
  • 390
  • 454
  • But the op is right, accessing the nth elenent of a linked list and dereferencing n levels of a pointer is (behind the curtains) the very same thing (modulo some pointer arithmetic). – Witiko Dec 17 '14 at 18:21
3

Let's put it in simpler terms.

Declare a variable - doesn't really matter what type - and it represents a location in memory.

int foo=1;

You can then declare another variable that points to that variable.

int *bar;
bar = &foo;

Extend it again - declare a pointer to that variable...and so on.

int *baz;
baz = &bar;

The point is there's no limit to the levels of indirection to which any given pointer might be used or declared. And, syntactically, you can do

int ****nthLevelPointer;

Now, keeping track of that in code in a manner someone else might have to maintain is another issue entirely :)

David W
  • 9,822
  • 30
  • 59
  • Thank you, that makes a lot of sense conceptually. I'm wondering though, when you create a pointer to a pointer as in your example int *baz... wouldn't the syntax be **baz; as opposed to *baz? I'm confused about when to use two stars instead of one. –  Dec 17 '14 at 17:33
3

A simple example:

struct List
{
    struct List* next ;
}

struct List a ;
struct List* p = &a ;
p->next = p ;
p = p->next ;
p = p->next->next ;
p = p->next->next->next ;
p = p->next->next->next->next ;
p = p->next->next->next->next->next ;
p = p->next->next->, ... ,next->next->next ;

shows that there is no theoretical limit to pointer indirection depth.

2501
  • 24,549
  • 4
  • 42
  • 83
1

Your answer is yes. pointers are just references to memory.so if we can reference to memory we can reference to pointer too. you can get them size, in heap of your process.you can define them by local variable too.assume this schema:

pointer a -----> pointer b -----> pointer c ------>(local variable or define variable in heap)

0

Short answer: Pointers point to addresses in memory, which may themselves hold more pointers. The limit is the number of addresses available.

Shorter answer: Yes, there can be a pointer-pointer-pointer.

Justin Reeves
  • 941
  • 9
  • 22
0

A pointer to a pointer to a pointer is possible.

if you declarate a variable like this:

int ***ptr;

then:

->    ptr will be a pointer to a pointer to a pointer to an int variable
->   *ptr will be a pointer to a pointer to an int variable
->  **ptr will be a pointer to an int variable
-> ***ptr will be a int variable

therefore:

highest pointer level:    ptr
     ...                 *ptr
     ...                **ptr
int variable           ***ptr
E C
  • 1
  • 1