在C中递增指针数组

时间:2021-07-08 01:31:05

this is probably an essentially trivial thing, but it somewhat escapes me, thus far..

这可能是一件非常微不足道的事情,但到目前为止,它有点让我感到震惊。

char * a3[2];
a3[0] = "abc";
a3[1] = "def";
char ** p;
p = a3;

char * a3 [2]; a3 [0] =“abc”; a3 [1] =“def”; char ** p; p = a3;

this works:

printf("%p - \"%s\"\n", p, *(++p));

printf(“%p - \”%s \“\ n”,p,*(++ p));

this does not:

这不是:

printf("%p - \"%s\"\n", a3, *(++a3));

printf(“%p - \”%s \“\ n”,a3,*(++ a3));

the error i'm getting at compilation is:

我在编译时遇到的错误是:

lvalue required as increment operand

lvalue需要作为增量操作数

what am i doing wrong, why and what is the solution for 'a3'?

我做错了什么,为什么以及'a3'的解决方案是什么?

5 个解决方案

#1


4  

a3 is a constant pointer, you can not increment it. "p" however is a generic pointer to the start of a3 which can be incremented.

a3是一个常量指针,你不能递增它。 “p”是指向a3开头的通用指针,可以递增。

#2


4  

You can't assign to a3, nor can you increment it. The array name is a constant, it can't be changed.

你不能分配给a3,也不能增加它。数组名称是常量,不能更改。

c-faq

#3


0  

Try

char *a3Ptr = a3;
printf("%p - \"%s\"\n", a3, *(++a3Ptr));

In C, a char array[] is different from char*, even if you can use a char* to reference the first location of an array of char.

在C中,char数组[]与char *不同,即使您可以使用char *来引用char数组的第一个位置。

aren't both "p" and "a3" pointers to pointers?

指针不是“p”和“a3”指针吗?

Yes but a3 is constant. You can't modify it.

是的,但a3是不变的。你无法修改它。

#4


0  

a3 is a name of an array. This about it as a constant pointer.

a3是数组的名称。这是关于它作为常量指针。

You cannot change it. You could use a3 + 1 instead of ++a3.

你无法改变它。您可以使用a3 + 1而不是++ a3。

Another problem is with the use of "%s" for the *(++a3) argument. Since a3 is an array of char, *a3 is a character and the appropriate format specifier should be %c.

另一个问题是对*(++ a3)参数使用“%s”。由于a3是char数组,* a3是一个字符,相应的格式说明符应为%c。

#5


0  

You cannot increment or point any char array to something else after creating. You need to modify or access using index. like a[1]

创建后,您不能将任何char数组增加或指向其他内容。您需要使用索引修改或访问。像一个[1]

#1


4  

a3 is a constant pointer, you can not increment it. "p" however is a generic pointer to the start of a3 which can be incremented.

a3是一个常量指针,你不能递增它。 “p”是指向a3开头的通用指针,可以递增。

#2


4  

You can't assign to a3, nor can you increment it. The array name is a constant, it can't be changed.

你不能分配给a3,也不能增加它。数组名称是常量,不能更改。

c-faq

#3


0  

Try

char *a3Ptr = a3;
printf("%p - \"%s\"\n", a3, *(++a3Ptr));

In C, a char array[] is different from char*, even if you can use a char* to reference the first location of an array of char.

在C中,char数组[]与char *不同,即使您可以使用char *来引用char数组的第一个位置。

aren't both "p" and "a3" pointers to pointers?

指针不是“p”和“a3”指针吗?

Yes but a3 is constant. You can't modify it.

是的,但a3是不变的。你无法修改它。

#4


0  

a3 is a name of an array. This about it as a constant pointer.

a3是数组的名称。这是关于它作为常量指针。

You cannot change it. You could use a3 + 1 instead of ++a3.

你无法改变它。您可以使用a3 + 1而不是++ a3。

Another problem is with the use of "%s" for the *(++a3) argument. Since a3 is an array of char, *a3 is a character and the appropriate format specifier should be %c.

另一个问题是对*(++ a3)参数使用“%s”。由于a3是char数组,* a3是一个字符,相应的格式说明符应为%c。

#5


0  

You cannot increment or point any char array to something else after creating. You need to modify or access using index. like a[1]

创建后,您不能将任何char数组增加或指向其他内容。您需要使用索引修改或访问。像一个[1]