在C中初始化char指针数组

用户名

我试图以char不同的方式创建一个指针数组,但是只有第一种方法有效:

#include <stdio.h> 

int main(){

 char* a[] = {"hello"};
 // works

 char** b = {"hello"}; 
 // warning: incompatible pointer types initializing 
 // 'char **' with an expression of type 'char [6]' 

 char c[][] = {"hello"};  
 // error: array has incomplete element type 'char []'

 return 0;
}

我究竟做错了什么?

Aplet123
  1. 由于两个元素都不是数组,因此C编译器无法将{"hello"}语法识别为数组,这将导致代码中断。如果这样做,char** b = a您会发现该语法确实有效。
  2. 在C语言中使用多维数组时,除第一个维外,每个维都必须指定长度,因为编译器无法推断出它。如果将其更改为char c[][6] = {"hello"}您可以观察到它的工作原理。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章