二位数组引用

玉怀一捧雪 2014-12-05 11:00:28
网上看到的
int (*pdat)[5] 等效int pdat[x][5]


const char code *TAB[4]={
{"Main Menu!"},

{"001--Menu!"},{"002--Menu!"},{"003--Menu!"},
};
char *p=TAB[0];


上面的代码能编译,但是我把字符串改成数组就出错了

const char code *TAB[4]={
{1,2,3},

{0,0,1},{0,0,2},{0,0,3},
};
char *p=TAB[0];

...全文
156 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
均陵鼠侠 2014-12-06
至于为啥上面的静态数组会转换为指向其第一个元素的指针,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.
回复
均陵鼠侠 2014-12-06
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].
回复
玉怀一捧雪 2014-12-05
引用 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]应该就指向下一行了呀
回复
zhouchongzxc 2014-12-05

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.
回复
羽飞 2014-12-05
楼主,你这样定义也会出错: const char *code = {1, 2, 3}; 更不用说数组了
回复
羽飞 2014-12-05
引用 5 楼 Vecent_ 的回复:

	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码
回复
羽飞 2014-12-05
引用 5 楼 Vecent_ 的回复:

	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码
回复
羽飞 2014-12-05
引用 5 楼 Vecent_ 的回复:

	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码
回复
Vecent_ 2014-12-05

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码

回复
Vecent_ 2014-12-05

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码

回复
Vecent_ 2014-12-05

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码

回复
相关推荐
发帖
C语言
创建于2007-09-28

6.3w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2014-12-05 11:00
社区公告
暂无公告