简体   繁体   English

数组指针的类型是什么?

[英]What is the type of array pointer?

When I declare an array in C: 当我在C中声明数组时:

char a[] = "something";

I understand that a is implicitly a const character pointer, ie a is of the type: (char * const) 我知道a是隐式的const字符指针,即a是以下类型: (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 *) . 我设法摆脱它的唯一方法是将右手侧显式转换为(char * const *)

I hypothesized that & operator returns a constant pointer and tried: 我假设&运算符返回一个常量指针并尝试:

char * const * const j = &a;

without success. 没有成功。

What is going on here? 这里发生了什么?

 char a[] = "something"; 

I understand that a is implicitly a const character pointer, ie a is of the type: (char * const) 我知道a是隐式的const字符指针,即a是以下类型: (char * const)

Wrong, a is of type (non-const) char[10] . 错误的, a是(非常量) char[10]

So now that we know a is char[10] , it's clear why the following doesn't compile: 因此,现在我们知道achar[10] ,很清楚为什么以下代码无法编译:

char * const * j = &a;

&a is of type char(*)[10] (ie pointer to char[10] ). &a的类型为char(*)[10] (即,指向char[10]指针)。 char(*)[10] and char * const * are completely unrelated. char(*)[10]char * const *完全无关。


If you wrote char* a = "something"; 如果您写了char* a = "something"; , then a would be a char* . ,则a将为char*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM