Why can’t I access a pointer to pointer for a stack array?

test is an array, not a pointer, and &test is a pointer to the array. It is not a pointer to a pointer.

You may have been told that an array is a pointer, but this is incorrect. The name of an array is a name of the entire object—all the elements. It is not a pointer to the first element. In most expressions, an array is automatically converted to a pointer to its first element. That is a convenience that is often useful. But there are three exceptions to this rule:

  • The array is the operand of sizeof.
  • The array is the operand of &.
  • The array is a string literal used to initialize an array.

In &test, the array is the operand of &, so the automatic conversion does not occur. The result of &test is a pointer to an array of 256 char, which has type char (*)[256], not char **.

To get a pointer to a pointer to char from test, you would first need to make a pointer to char. For example:

char *p = test; // Automatic conversion of test to &test[0] occurs.
printchar(&p);  // Passes a pointer to a pointer to char.

Another way to think about this is to realize that test names the entire object—the whole array of 256 char. It does not name a pointer, so, in &test, there is no pointer whose address can be taken, so this cannot produce a char **. In order to create a char **, you must first have a char *.

Leave a Comment

techhipbettruvabetnorabahisbahis forumu