有时候可以见到这样的例子:
char *color_name[] = {
“black”,
“blue”,
…
};
我们知道字符串常量就是一个char数组,但是为什么数组可以用于给指针数组的元素赋值呢?这个操作的依据是什么呢?
https://stackoverflow.com/questions/7690340/when-does-a-array-act-as-a-pointer-in-c
主要来自C标准里面的这样一句话:
… Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type “array of type” is converted to an expression with type “pointer to type” that points to the initial element of the array object and is not an lvable.
也就是说,在上述例子中,字符串常量会被转换为指向char类型的指针。