C语言结构体赋值

声明:

    本文是我在工作中遇到的C语言结构体赋值以及C语言指针问题后的一些总结,希望可以对你有所帮助。

介绍:

 

先不做讲解,看看下面的代码,大家猜一下打印的值,看大家对C语言结构体赋值和结构体指针是不是了解:

#include 
int main () 
{
	typedef struct test {
		int a;
		int b;
		char c[20];
	}TEST;

	TEST a1 = {1,2,"xiang"};
	TEST b1 = {3,4,"jing"};
	TEST *c1;
	TEST *d1 = NULL;
	
	printf("a = %d, b = %d , c = %s \n", a1.a, a1.b, a1.c);
	printf("a = %d, b = %d , c = %s \n", b1.a, b1.b, b1.c);
	*c1 = a1;
	printf("\\\\\\\\\\\\\\\\\\\\ *c1 = a1 \\\\\\\\\\\\\\\\\\\\\ \n");	
	printf("a = %d, b = %d , c = %s \n", a1.a, a1.b, a1.c);
	printf("a = %d, b = %d , c = %s \n", b1.a, b1.b, b1.c);
	printf("a = %d, b = %d , c = %s \n", c1->a, c1->b, c1->c);
	
	c1->a = 6;
	c1->b = 8;
	printf("\\\\\\\\\\\\\\\\\\\*c1 = a1 c1->a = 6 c1->b = 8 \\\\\\\\\\\\\\\\\\\\\\ \n");	
	printf("a1  a = %d, b = %d , c = %s \n", a1.a, a1.b, a1.c);
	printf("b1  a = %d, b = %d , c = %s \n", b1.a, b1.b, b1.c);
	printf("c1  a = %d, b = %d , c = %s \n", c1->a, c1->b, c1->c);
	printf("c1's addr is 0x%p a1's addr is 0x%p  \n", c1, &a1);
 
	d1 = &b1;
	printf("\\\\\\\\\\\\\\\\\d1 = &b1\\\\\\\\\\\\\\\\\\\\\\\\ \n");	
	printf("a1  a = %d, b = %d , c = %s \n", a1.a, a1.b, a1.c);
	printf("b1  a = %d, b = %d , c = %s \n", b1.a, b1.b, b1.c);
	printf("c1  a = %d, b = %d , c = %s \n", c1->a, c1->b, c1->c);	
	printf("d1  a = %d, b = %d , c = %s \n", d1->a, d1->b, d1->c);	

	d1->a = 5;
	d1->b = 7;
	printf("\\\\\\\\\\\\\\\\d1 = &b1 d1->a = 5  d1->b = 7\\\\\\\\\\\\\\\\\\\\\\\\\ \n");	
	printf("a1  a = %d, b = %d , c = %s \n", a1.a, a1.b, a1.c);
	printf("b1  a = %d, b = %d , c = %s \n", b1.a, b1.b, b1.c);
	printf("c1  a = %d, b = %d , c = %s \n", c1->a, c1->b, c1->c);	
	printf("d1  a = %d, b = %d , c = %s \n", d1->a, d1->b, d1->c);
	printf("c1's addr is 0x%p a1's addr is 0x%p  \n", c1, &a1);
	printf("d1's addr is 0x%p b1's addr is 0x%p  \n", d1, &b1);
	return 0;
}

对应的log为:

a = 1, b = 2 , c = xiang 
a = 3, b = 4 , c = jing 
\\\\\\\\\\ *c1 = a1 \\\\\ 这里就是结构体赋值,从结果看c1获得了与a1一样的值,似乎这里的操作与将c1指向a1 的操作相似
a = 1, b = 2 , c = xiang 
a = 3, b = 4 , c = jing 
a = 1, b = 2 , c = xiang 
\\\\\\\\\*c1 = a1 c1->a = 6 c1->b = 8 \\\\\\\\\\\  但是改变c1的值之后,就可以看出与指向a1的区别了
a1  a = 1, b = 2 , c = xiang 
b1  a = 3, b = 4 , c = jing 
c1  a = 6, b = 8 , c = xiang   //这里只有c1的值改变了,但是a1的值并没有改变
c1's addr is 0x0x7ffdd1610b88 a1's addr is 0x0x7ffdd1610a30    
\\\\\\\\d1 = &b1\\\\\\\\\\\\   这里是将d1的值指向b1 ,d1的值与b1一样
a1  a = 1, b = 2 , c = xiang 
b1  a = 3, b = 4 , c = jing 
c1  a = 6, b = 8 , c = xiang 
d1  a = 3, b = 4 , c = jing 
\\\\\\\\d1 = &b1 d1->a = 5  d1->b = 7\\\\\\\\\\\\   而改变d1的值的时候,b1的值也随之改变
a1  a = 1, b = 2 , c = xiang 
b1  a = 5, b = 7 , c = jing    //改变d1的值,b1的值也随之改变
c1  a = 6, b = 8 , c = xiang 
d1  a = 5, b = 7 , c = jing 
c1's addr is 0x0x7ffdd1610b88 a1's addr is 0x0x7ffdd1610a30  
d1's addr is 0x0x7ffdd1610a50 b1's addr is 0x0x7ffdd1610a50

结论为:

1. 使用C语言结构体赋值 可以获得赋值参数的值,但是之后赋值参数的修改(或者被赋值参数的修改)与被赋值参数(或者赋值参数)无关。因为这里赋值的与被赋值的不在同一空间,所以他们值得改变互不影响。

2. 使用C语言结构体指针,可以将指针指向参数空间,这样指向的参数修改(或者被指向的参数修改)都会改变对方的值。或者双方的值都将随之改变。因为他们指向的是同一块空间,要变一起变。

你可能感兴趣的:(C语言,c语言,指针,结构体,赋值,地址)