How many consecutive nested pointer (pointer to pointer) can i have in one order? Is there a limit of doing references?

1.5k views Asked by At

The follow program declares a pointer then again a new pointer to hold address of previous pointer variable.. How much can I use nested pointer variable to hold memory address is there any limit?

#include <stdio.h>
#include <conio.h>

void main()
{
    int x=2,y=5;

    int *ptr;
    int **sptr;
    int ***ssptr;

    ptr = &x; // address of x
    *ptr = 0; 
    sptr = &ptr;
    ssptr = & sptr;

printf(" address is ip = %u %u %u",ptr,sptr,ssptr);
    _getch();
}
6

There are 6 answers

0
Daniel Gallagher On BEST ANSWER

There is no limit. A pointer is a chunk of memory (typically one word) whose contents are an address. A pointer to a pointer is also a word whose contents are an address, but it just so happens that the contents at that address is another address. There is nothing particularly special about a pointer to a pointer (to a pointer to a pointer... etc., ad nauseum).

0
Argote On

As far as I know, there shouldn't be any limit except for system memory restrictions (in theory). This would depend on the compiler used though.

0
David Weiser On

There is not a limit in the language itself. The purpose of a pointer variable is to store an address. It is possible to store a pointer which points to an address, which points to an address, ..., which points to an address.

However, the more you use these types of nested pointers, the less understandable your code will be.

0
Todd On

No there is no limit because they are all just pointers to something, and the thing they point to just happens to be another pointer. Are you trying to do something practical? Todd.

0
caf On

There is no limit. You can even make a pointer that points at itself, which is infinitely recursive:

void *p = &p;
0
John Bode On

The only language I could find that suggests a limit is the following:

5.2.4.1 Translation limits

1 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:13)
...
— 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or incomplete type in a declaration
...
— 4095 characters in a logical source line
...