user-0 提问: 二位数组引用

回复:
user: user-1
content: 至于为啥上面的静态数组会转换为指向其第一个元素的指针，N1570，6.3.2.1  Lvalues, arrays, and function designators， 3) Except when it is the operand of the sizeof operator, the_Alignof 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 lvalue.

user: user-1
content: N1570，6.7.9 Initialization，17) Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union. 这意味着，对于声明 char * TAB [3] = {5, 6, 7}; 5对应于TAB[0]；6对应于TAB[1]，而7对应于TAB[2]。 进一步地，对于声明 char * TAB [3] = {{5,6}, {6,7}, {7,8}}; {5,6}对应于TAB[0]；{6,7}对应于TAB[1]，而{7,8}对应于TAB[2]。  N1570，6.7.9 Initialization，2) No initializer shall attempt to provide a value for an object not contained within the entity being initialized. 这意味着，对于声明 char * TAB [3] = {{5,6}, {6,7}, {7,8}}; {5,6}中，只有5对应TAB[0]，其它依次类推。  N1570，6.7.9 Initialization，11)The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type. 这意味着，对于声明 char * TAB [3] = {{5,6}, {6,7}, {7,8}}; 5、6和7需要转换成指针类型（char *）分别用于初始化TAB[0]、TAB[1]和TAB[2]。但是我们看到，编译器会警告。  N1570，6.4.5 String literals，6 In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence.  因此，对于声明 char * TAB [3] = {"hello", "world, "!!!!!"}; 串字面值将先用于初始化一个静态数组。然后，因为TAB的元素类型是char *，所以，这些静态数组将转换为指向它们第一个元素的指针。最后，用这些指针的值初始化TAB[0]、TAB[1]，以及TAB[2].

user: user-0
content: 引用 1 楼 hnwyllmm 的回复:楼主，你这样定义也会出错： const char *code = {1, 2, 3}; 更不用说数组了  请问更改。  const char code TAB[4][3]={                         {1,2,3},                                                                       {0,0,1},{0,0,2},{0,0,3}, }; char *p=TAB[0];  我希望的是*p++指向的是第一行3个的元素，可是我上面char *p=TAB[0]应该就指向下一行了呀

user: user-2
content: int main(){    const int *a = {6,8,0}; }    bash-3.2$ clang t.c t.c:4:19: warning: incompatible integer to pointer conversion initializing 'const int *' with an expression of type 'int' [-Wint-conversion]   const int *a = {6,8,0};                   ^ t.c:4:21: warning: excess elements in scalar initializer   const int *a = {6,8,0};                     ^ 2 warnings generated.

user: user-3
content: 楼主，你这样定义也会出错： const char *code = {1, 2, 3}; 更不用说数组了

user: user-3
content: 引用 5 楼 user-4 的回复: 	const char TAB[4][3]={ 		{1,2,3}, 		{0,0,1},{0,0,2},{0,0,3}, 	}; 	const char *p[4]; 	int i; 	for (i=0;i <4; i++) 	{ 		p[i] = TAB[i]; 	} 	for (i=0; i<4; i++) 	{ 		cout<<*p[i]<<" "<<*(p[i]+1)<<" "<<*(p[i]+2)<<endl; 	}  上面p[i]表示指向数组第i行 如果打印为整形数据，输出结果： 1 2 3 0 0 1 0 0 2 0 0 3 打印字符型，结果为对应的ASCII码 对

user: user-3
content: 引用 5 楼 user-4 的回复: 	const char TAB[4][3]={ 		{1,2,3}, 		{0,0,1},{0,0,2},{0,0,3}, 	}; 	const char *p[4]; 	int i; 	for (i=0;i <4; i++) 	{ 		p[i] = TAB[i]; 	} 	for (i=0; i<4; i++) 	{ 		cout<<*p[i]<<" "<<*(p[i]+1)<<" "<<*(p[i]+2)<<endl; 	}  上面p[i]表示指向数组第i行 如果打印为整形数据，输出结果： 1 2 3 0 0 1 0 0 2 0 0 3 打印字符型，结果为对应的ASCII码 对

user: user-3
content: 引用 5 楼 user-4 的回复: 	const char TAB[4][3]={ 		{1,2,3}, 		{0,0,1},{0,0,2},{0,0,3}, 	}; 	const char *p[4]; 	int i; 	for (i=0;i <4; i++) 	{ 		p[i] = TAB[i]; 	} 	for (i=0; i<4; i++) 	{ 		cout<<*p[i]<<" "<<*(p[i]+1)<<" "<<*(p[i]+2)<<endl; 	}  上面p[i]表示指向数组第i行 如果打印为整形数据，输出结果： 1 2 3 0 0 1 0 0 2 0 0 3 打印字符型，结果为对应的ASCII码 对

user: user-4
content: const char TAB[4][3]={ 		{1,2,3}, 		{0,0,1},{0,0,2},{0,0,3}, 	}; 	const char *p[4]; 	int i; 	for (i=0;i <4; i++) 	{ 		p[i] = TAB[i]; 	} 	for (i=0; i<4; i++) 	{ 		cout<<*p[i]<<" "<<*(p[i]+1)<<" "<<*(p[i]+2)<<endl; 	}  上面p[i]表示指向数组第i行 如果打印为整形数据，输出结果： 1 2 3 0 0 1 0 0 2 0 0 3 打印字符型，结果为对应的ASCII码

user: user-4
content: const char TAB[4][3]={ 		{1,2,3}, 		{0,0,1},{0,0,2},{0,0,3}, 	}; 	const char *p[4]; 	int i; 	for (i=0;i <4; i++) 	{ 		p[i] = TAB[i]; 	} 	for (i=0; i<4; i++) 	{ 		cout<<*p[i]<<" "<<*(p[i]+1)<<" "<<*(p[i]+2)<<endl; 	}  上面p[i]表示指向数组第i行 如果打印为整形数据，输出结果： 1 2 3 0 0 1 0 0 2 0 0 3 打印字符型，结果为对应的ASCII码

user: user-4
content: const char TAB[4][3]={ 		{1,2,3}, 		{0,0,1},{0,0,2},{0,0,3}, 	}; 	const char *p[4]; 	int i; 	for (i=0;i <4; i++) 	{ 		p[i] = TAB[i]; 	} 	for (i=0; i<4; i++) 	{ 		cout<<*p[i]<<" "<<*(p[i]+1)<<" "<<*(p[i]+2)<<endl; 	}  上面p[i]表示指向数组第i行 如果打印为整形数据，输出结果： 1 2 3 0 0 1 0 0 2 0 0 3 打印字符型，结果为对应的ASCII码