Exploration on the reason why arrays cannot be directly assigned in C language

#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;
}	

As shown in the above code, the structure name is the same as the ordinary variable name, and the address needs to be obtained through &, which is a variable; and the array name itself represents the address, which is the address constant, as if it is illegal &stru2 = &stru. It is also impossible to assign a value. Furthermore, array is a constant, while array[0] is a variable, which is why array and &array also represent addresses.

Guess you like

Origin blog.csdn.net/watershed1993/article/details/115696450