【结构体】:struct为结构体关键字,Student为结构体的标志,student是变量名
①读取结构体内变量时:
#include <stdio.h>
struct Student {
int age;
int id;
char name;
}
当变量是普通变量:
int main(){
struct Student student={18,1,"Estella"};
printf("%d",student.age);//即直接打印年龄
}
当变量是指针类型时:
int main(){
struct Student student={18,1,"EStella"}
struct Student *p=&student;
printf("%d",p->age); //或者是printf("%d",(*p).age);
}
②结构体作为参数在函数中传递:
(仅仅也是值传递)
传递普通变量时,不改变值,仅在该函数内有效
#include <stdio.h>
struct student {
int age;
char* name;
};
void text(struct student A){
A.age=19;
}
int main() {
struct student A = {18,"Estella"};
text(A);
printf("%d",A.age);
}
结果为 18
传递指针时,改变值
#include <stdio.h>
struct student {
int age;
char* name;
};
void text(struct student *A){
A->age=19;
}
int main() {
struct student A = {18,"Estella"};
text(&A);
printf("%d",A.age);
}
结果为 19
使用指针进行传递可以节省空间,提高效率