#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) 一样赋值 =, 做函数参数。
通过变量复制(复制内存) 或者 通过指针。