关于C语言中数组不能直接赋值的原因探究

#include <stddef.h>
#include <stdio.h>


typedef struct{
       int s1;
       int s2;
}Struct;


int test()
{
	return 0;
}

int test2()
{
	return 0;
}
	
int main()
{
	int array[3] = {1,2,3};
	Struct stru = {1,2};
	Struct sru2 = stru;
	int a = 5;
    void* p = NULL;

	p = array;
	printf("%x\n", p);
	p = test;
	printf("%x\n", p);
	p = &stru;
	printf("%x\n", p);
	p = &a;
	printf("%x\n", p);

	return 0;
}	

如上述代码所表示的,结构体名跟普通变量名一样,需要通过&来取地址,其是变量;而数组名和函数名一样本身就代表地址,是地址常量,如同&stru2 = &stru非法一样,数组名之间也无法赋值,进一步说,array是常量,而array[0]则是变量,这也是array与&array同样表示地址的原因。

猜你喜欢

转载自blog.csdn.net/watershed1993/article/details/115696450