目录
最近两次讲的内容是结构体。(对于统计学的学生来说,这个跟R语言里面的数据框差不多)。 数据类型有内置类型和自定义数据类型。
一、结构体的一般知识
自定义数据类型 struct结构体
1、哪些类型可以出现在结构体内部。
内置类型,在使用前已经定义好的自定义类型,定义一个指向自己的指针
struct Date
{
int year;
int month;
int day;
};
struct Student //设计Student这种类型,数据类型只要设计好之后,就等同于内置类型
{
char name[20];
int age;
char sex;//
char addr[100];
struct Date bir;//
//struct A aa;//error,无法确定内存大小
//struct Student sa;//error,无法确定内存大小,结构体定义一个自己的普通变量作为成员错误
struct Student *ps;//ok
int a;
};
struct A
{
int a;
int b;
};
//内置类型
int main()
{
int a = 10;
int arr[10] = {1,2,3,4,5};
int *p = &a;
struct Student b;
struct Student brr[10];
struct Student *ps = &b;
return 0;
}
2、哪些名字能使用
结构体普通变量通过“.”号访问它的成员变量
结构体指针变量通过“->”访问它的成员变量
struct A
{
int a;
int b;
};
struct Student
{
char name[20];
int age;
};
int main()
{
int a;
int arr[10];
int *p = &a;
struct A sa = {50};//剩余的赋值为0
sa.b = 100;//
sa.a = 500;
struct A *pa = &sa;
//a=200,b=300
(*pa).a = 200;
pa->b = 300;
printf("%d,%d\n",sa.a,sa.b);
struct Student stu1 = {"liubei",30};
//将stu1的值改为"caocao",25
//name,age
strcpy(stu1.name,"caocao");
stu1.age = 25;
printf("%s,%d\n",stu1.name,stu1.age);
//char name1[20];
//name1 = "caocao";
return 0;
}
3、typedef类型重定义