struct 赋值 做函数参数

#include <stdio.h>
#include <stdlib.h>

struct date{
	int year;
	int month;
	int day;
};

typedef struct date date;

date birth_date = {2012, 9, 26};

void struct_by_value(date date_copy){
	date_copy.year = 2000;
	printf("date_copy.year = %d\n", date_copy.year);
}

void struct_by_ptr(date *date_ptr){
	date_ptr->year = 3000;
	printf("date_ptr->year = %d\n", date_ptr->year);
}

void test_by_value_ptr_to_func(){
	printf("birth_date.year = %d\n", birth_date.year);
	struct_by_value(birth_date);
	printf("after by copy, birth_date.year = %d\n", birth_date.year);
	struct_by_ptr(&birth_date);
	printf("after by ptr, birth_date.year = %d\n", birth_date.year);
}

void test_by_value_ptr_equal(){
	printf("birth_date.year = %d\n", birth_date.year);
	date another_date = birth_date;
	another_date.year = 2013;
	printf("after value equals, birth_date.year = %d\n", birth_date.year);
	date *a_date_ptr = &birth_date;
	a_date_ptr->year = 4000;
	printf("after ptr equals, birth_date.year = %d\n", birth_date.year);
}


int main(void){
	test_by_value_ptr_to_func();
	puts("==========================");
	test_by_value_ptr_equal();
	puts("==========================");
}


结构体 变量可以像常规类型的变量(int a) 一样赋值 =, 做函数参数。

通过变量复制(复制内存) 或者 通过指针。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值