Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36671

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

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();
}

Upvotes: 2

Views: 1681

Answers (6)

caf
caf

Reputation: 239251

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

void *p = &p;

Upvotes: 1

John Bode
John Bode

Reputation: 123558

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
...

Upvotes: 0

Todd
Todd

Reputation: 97

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.

Upvotes: 0

Daniel Gallagher
Daniel Gallagher

Reputation: 7125

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).

Upvotes: 1

David Weiser
David Weiser

Reputation: 5205

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.

Upvotes: 0

Argote
Argote

Reputation: 2155

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.

Upvotes: 0

Related Questions