top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How many levels can we have in defining pointers?

+7 votes
447 views
How many levels can we have in defining pointers?
posted Dec 24, 2013 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Since a pointer to a pointer is again, well, just a pointer, there shouldn't be any theoretical limit. Maybe the compiler won't be able to handle it beyond some ridiculously high limit.

Check this (http://c0x.coding-guidelines.com/5.2.4.1.html) which says 12.
279 — 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or incomplete type in a declaration

answer Dec 24, 2013 by Salil Agrawal
+1 vote

The answer depends on what you mean by "levels of pointers." If you mean "How many levels of indirection can you have in a single declaration?" the answer is "At least 12."

int i = 0;
int *ip01 = & i;
int **ip02 = & ip01;
int ***ip03 = & ip02;
int ****ip04 = & ip03;
int *****ip05 = & ip04;
int ******ip06 = & ip05;
int *******ip07 = & ip06;
int ********ip08 = & ip07;
int *********ip09 = & ip08;
int **********ip10 = & ip09;
int ***********ip11 = & ip10;
int ************ip12 = & ip11;
************ip12 = 1; /* i = 1 */

If you mean "How many levels of pointer can you use before the program gets hard to read," that's a matter of taste, but there is a limit. Having two levels of indirection (a pointer to a pointer to something) is common. Any more than that gets a bit harder to think about easily; don't do it unless the alternative would be worse.

If you mean "How many levels of pointer indirection can you have at runtime," there's no limit. This point is particularly important for circular lists, in which each node points to the next. Your program can follow the pointers forever.

Consider the following program "A circular list that uses infinite indirection".

/* Would run forever if you didn't limit it to MAX */

struct circ_list
{
        char    value[ 3 ];     /* e.g., "st" (incl '\0') */
        struct circ_list        *next;
};
struct circ_list    suffixes[] = {
        "th", & suffixes[ 1 ], /* 0th */
        "st", & suffixes[ 2 ], /* 1st */
        "nd", & suffixes[ 3 ], /* 2nd */
        "rd", & suffixes[ 4 ], /* 3rd */
        "th", & suffixes[ 5 ], /* 4th */
        "th", & suffixes[ 6 ], /* 5th */
        "th", & suffixes[ 7 ], /* 6th */
        "th", & suffixes[ 8 ], /* 7th */
        "th", & suffixes[ 9 ], /* 8th */
        "th", & suffixes[ 0 ], /* 9th */
        };

int main()
{
     int i = 0;
     struct circ_list    *p = suffixes;
     while (i <= MAX) 
     {
             printf( "%d%s\n", i, p->value );
             ++i;
             p = p->next;
     }
     return 0;
}

Each element in suffixes has one suffix (two characters plus the terminating NUL character) and a pointer to the next element. next is a pointer to something that has a pointer, to something that has a pointer, ad infinitum.

The example is dumb because the number of elements in suffixes is fixed. It would be simpler to have an array of suffixes and to use the i%10'th element. In general, circular lists can grow and shrink.

answer Dec 30, 2013 by Sandeep
...