What is the limit to the number of pointers to pointers we can have in C?

The standard does not impose any upper limit. What it does say is that a compiler needs to support at least 12.

In practical code it can be considered infinite. Only if you write programs that writes programs that no human should ever read could this be relevant. Most coders would say that you should take three stars as a warning. Don't go beyond two without a really good reason.

I tried with 10000 on gcc and it worked. I'm now trying with 100000. One interesting thing is that it takes extremely long time to compile. It took several minutes to compile, and the only statement was a pointer declaration with 10000 stars.

Code to generate a C file:

// gen.c
#include <stdio.h>

int main()
{
    const size_t n = 10000;
    printf("int main(){int ");
    for(size_t i=0; i<n; i++)
        printf("*");
    printf("p;}\n");
}

Run:

$ gcc gen.c -c gen
$ ./gen > stars.c
$ gcc stars.c

Answer to comments:

This was a fun experiment, but I will not investigate this further.


The C 11 standard imposes no maximum limits and in fact states "Implementations should avoid imposing fixed translation limits whenever possible." in a footnote.

The minimum limits are given by 5.2.4 Environmental limits:

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:18)

  • 127 nesting levels of blocks
  • 63 nesting levels of conditional inclusion
  • 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration
  • 63 nesting levels of parenthesized declarators within a full declarator
  • 63 nesting levels of parenthesized expressions within a full expression
  • 63 significant initial characters in an internal identifier or a macro name (each universal character name or extended source character is considered a single character)
  • 31 significant initial characters in an external identifier (each universal character name specifying a short identifier of 0000FFFF or less is considered 6 characters, each universal character name specifying a short identifier of 00010000 or more is considered 10 characters, and each extended source character is considered the same number of characters as the corresponding universal character name, if any)
  • 4095 external identifiers in one translation unit
  • 511 identifiers with block scope declared in one block
  • 4095 macro identifiers simultaneously defined in one preprocessing translation unit
  • 127 parameters in one function definition
  • 127 arguments in one function call
  • 127 parameters in one macro definition
  • 127 arguments in one macro invocation
  • 4095 characters in a logical source line
  • 4095 characters in a string literal (after concatenation)
  • 65535 bytes in an object (in a hosted environment only)
  • 15 nesting levels for #included files
  • 1023 case labels for a switch statement (excluding those for any nested switch statements)
  • 1023 members in a single structure or union
  • 1023 enumeration constants in a single enumeration
  • 63 levels of nested structure or union definitions in a single struct-declaration-list

A conforming C compiler would provide at least 12 levels of pointer indirection.