在C语言中,数组是不能递增的

在C语言中,数组是不能递增的

#include<stdio.h>
void main()
{
	int *p,*q,a[10]={1,2,3,4,5,6,7,8,9,0};
	p=a;
        printf("--%d_\n",*p++);
        printf("--%d_\n",*++p);
	printf("--%d_\n",2[a]);
	printf("--%d_\n",2[a]);
        printf("--%d_\n",*(a+2));
        printf("--%d_\n",*(2+a));
        printf("--%d_\n",*a+1));
        printf("--%d_\n",*a++));
 } 

 通过上述的例子,我们可以发现:

printf("--%d_\n",*a++); 这里错了
因为数组名是一个地址常量 不是指针,不是变量。您不能通过a++(a=a+1)去修改a数组地址
[Error] lvalue required as increment operand 左式只能是变量不能是表达式   所以并不是我们想的那种 (*a+1)

Most likely you fell victim to a popular misconception that "array is a pointer", 

i.e. when you define an array what you actually get is an ordinary pointer that points to some block of memory allocated somewhere. 

In your code you are making an attempt to increment that pointer.

意思是说 很多人认为数组是一个指针 其实是不对的,数组最多只能算是一个指针常量,同时数组递增说法也是在C语言中不存在的!!!

猜你喜欢

转载自blog.csdn.net/qq_37457202/article/details/82766811