定义字符数组char 和int 定义的方法

img


为什么第一个是对的呢我记得定义三种模式没这一种啊第二个为什么是错的呢

字符串字面常量只能初始化相应类型字符数组

char str[] = "abc"; // str has type char[4] and holds 'a', 'b', 'c', '\0'
wchar_t wstr[4] = L"猫"; // str has type wchar_t[4] and holds L'猫', '\0', '\0', '\0'
char str[3] = "abc"; // str has type char[3] and holds 'a', 'b', 'c'

所有类型的数组(包括字符数组)都可以用花括号来初始化

int x[] = {1,2,3}; // x has type int[3] and holds 1,2,3
int y[5] = {1,2,3}; // y has type int[5] and holds 1,2,3,0,0
int z[3] = {0}; // z has type int[3] and holds all zeroes

https://en.cppreference.com/w/c/language/array_initialization

你就不能写出来吗,问个问题也要偷懒。

  • 有的,这种声明方式要求大括号中的必须是单引号括起来的字符
  • 第二种声明方式对char型数组是正确的,但是你写的第二种是int型数组,int型数组不可以使用双引号表示的字符串进行初始化

    img

声明数组必须用大括号,字符串才可以用双引号