当前位置:首页 > 通信资讯 > 正文

c语言结构体指针例题(C语言指针结构体)

写结构体指针前,先说一下.号和->的区别

记得当初刚学C语言的时候,搞不清结构体的 . 号和 ->,经常混淆二者的使用。

那么在C语言中. 号是成员访问运算符,当我们需要访问结构的成员的时候,就会使用到它

而当我们需要使用结构体指针来访问结构成员的时候,就得使用->运算符了

结构体指针栗子:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ​#include<stdio.h> #include<string.h> typedef struct student{ int id; char name[10]; char sex; }stu; //结构体别名 void PrintStu(stu *student); int main() { //结构体对象 stu stu1; printf("sizeof of stu1 is:%d\n",sizeof(stu1)); stu1.id=2014; strcpy(stu1.name,"zhangfei"); stu1.sex='m'; PrintStu(&stu1); printf("***************\n"); //结构体指针 stu *s = (stu*)malloc(sizeof(stu)); //申请堆内存 s->id = 2018; strcpy(s->name, "zhangfei"); s->sex = 'g'; PrintStu(s); return 0; } void PrintStu(stu *student) { printf("stu1 id is :%d\n",student->id); printf("stu1 name is :%s\n",student->name); printf("stu1 sex is :%c\n",student->sex); }

结构体指针,就是指向结构体的指针。

解释C函数中的形参:

void PrintStu(stu *student)中的形参stu *student,说通俗点就是用来接住外部传来的地址&stu1。

即 stu *student=&stu1; student可以取其他名字,形参并不是固定的。

到此这篇关于C语言结构体指针案例解析的文章就介绍到这了,更多相关C语言结构体指针案例内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/c_shell_python/article/details/80064680

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。