程序员之路:C语言typedef和struct
来源:互联网 发布:现在在哪里看日剧 知乎 编辑:程序博客网 时间:2024/06/16 16:37
学习C语言的时候,发现typedef和struct这个有点迷糊,继续学习,发现也不是特别难,正好抽时间总结一下。
1、首先看个例子:
//typedef与struct#include <stdio.h>#include <string.h> //使用strcpy();//结构定义,Student是一个Tag标签,区分其他结构struct Student{ char name[50]; int age; float score;};int main(){ // 声明 struct Student student; // 使用赋值 strcpy(student.name,"Tom"); student.age=25; student.score=99.0; // 使用读取 printf("student.name : %s\n",student.name); printf("student.age : %d\n",student.age); printf("student.score : %.2f\n",student.score); return 0;}
不难看出,我在main函数之前定义了一个struct Student结构,存储学生的姓名,年龄,分数
注意:struct Student结构大括号{}后面有一个分号“;”,相当于一条语句。
main函数中,对struct Student结构进行了使用
2、下面继续:
//typedef与struct#include <stdio.h>#include <string.h> //使用strcpy();//结构定义,Student是一个Tag标签,区分其他结构struct Student{ char name[50]; int age; float score;} student;//变量int main(){ // 使用赋值 strcpy(student.name,"Tom"); student.age=25; student.score=99.0; // 使用读取 printf("student.name : %s\n",student.name); printf("student.age : %d\n",student.age); printf("student.score : %.2f\n",student.score); return 0;}
这个例子,和第1个例子中,区别在于:
(1)struct Student结构大括号后面多了一个student(注意大小写,c语言区分大小写);
(2)main函数中,我并没有单独声明student,就直接使用了。其实,在定义的时候,student(小写)就是声明的变量;
其实这两种方式是一样的。
3、看第三个例子
//typedef与struct#include <stdio.h>#include <string.h> //使用strcpy();//结构定义,Student是一个Tag标签,区分其他结构typedef struct Student{ char name[50]; int age; float score;} Student;//别名int main(){ //申明 Student student; // 使用赋值 strcpy(student.name,"Tom"); student.age=25; student.score=99.0; // 使用读取 printf("student.name : %s\n",student.name); printf("student.age : %d\n",student.age); printf("student.score : %.2f\n",student.score); return 0;}
例子中,多加了一个typedef,相当于给struct Student 取了一个别名:Student,这个例子就和第1个例子很像了,只是申明的时候少写了一个struct
例2,和例3,同样在struct大括号后面写的字符串,例2表示:变量,例3表示:别名
4、当然,也可以使用指针
//typedef与struct#include <stdio.h>#include <string.h> //使用strcpy();//结构定义,Student是一个Tag标签,区分其他结构typedef struct Student{ char name[50]; int age; float score;} Student;//别名int main(){ //申明 Student student; Student *pStudent=&student; // 使用赋值 strcpy(pStudent->name,"Tom"); pStudent->age=25; pStudent->score=99.0; // 使用读取 printf("student.name : %s\n",pStudent->name); printf("student.age : %d\n",pStudent->age); printf("student.score : %.2f\n",pStudent->score); return 0;}
好了,先写到这里,以后再补充,欢迎大家批评指正。
欢迎交流 博主QQ:1940607002
阅读全文
0 0
- 程序员之路:C语言typedef和struct
- C语言中的struct和typedef struct
- C语言中 typedef struct 和 struct
- 基础C语言之Typedef和struct的结合使用
- 关于 C语言中typedef struct 和 struct
- C语言中struct和typedef struct的区别
- C语言struct和typedef struct的区别
- 黑马程序员--C语言之static和typedef
- C 语言和C++语言的 struct对比 & struct和typedef struct
- c语言:typedef + struct + 指针
- c语言:typedef + struct + 指针
- C语言typedef struct详解
- [C/C++] struct和typedef
- C/C++ struct和typedef struct
- C/C++中struct和typedef struct
- c/c++ 中的 struct和typedef struct
- C/C++的struct和typedef struct
- c语言和c++中的struct和typedef struct的异同
- C语言实现BF算法
- POJ 3468:A Simple Problem with Integers
- js爬虫拿到的字符串数据转化成数组再转化成json对象
- 99乘法表
- 图解 Android 事件分发机制
- 程序员之路:C语言typedef和struct
- 如何生成转储(dmp)文件--工具篇
- Java枚举类
- AOP面向切面编程--高级
- iOS3DTouch功能实现
- 2017年上半年学习计划
- 制保留2位小数
- H5新特性
- EXT.NET NumberField 数字输入框