Akay
Akay

Reputation: 225

What is the type of array pointer?

When I declare an array in C:

char a[] = "something";

I understand that a is implicitly a const character pointer, i.e. a is of the type: (char * const)

But then why does the following statement result in the compiler warning about incompatible pointer types?

char * const * j = &a;

The only way I've managed to get rid of it is by explicitly casting the right hand side to (char * const *).

I hypothesized that & operator returns a constant pointer and tried:

char * const * const j = &a;

without success.

What is going on here?

Upvotes: 2

Views: 89

Answers (1)

flogram_dev
flogram_dev

Reputation: 42858

char a[] = "something";

I understand that a is implicitly a const character pointer, i.e. a is of the type: (char * const)

Wrong, a is of type (non-const) char[10].

So now that we know a is char[10], it's clear why the following doesn't compile:

char * const * j = &a;

&a is of type char(*)[10] (i.e. pointer to char[10]). char(*)[10] and char * const * are completely unrelated.


If you wrote char* a = "something";, then a would be a char*.

Upvotes: 4

Related Questions