c 语言结构体struct的三种定义方式 及 typedef

struct 结构体名{

成员列表;

.....

}结构体变量;

结构体类型变量的定义

结构体类型变量的定义与其它类型的变量的定义是一样的,但由于结构体类型需要针对问题事先自行定义,所以结构体类型变量的定义形式就增加了灵活性,共计有三种形式,分别介绍如下:

1) 先定义结构体类型,再定义结构体类型变量:
struct stu / *定义学生结构体类型* /
{
     char name[20]; / * 学生姓名* /
     char sex; / * 性别* /
     long num; / *学号* /
     float score[3]; / * 三科考试成绩* /
};
struct stu student1,student2;/ * 定义结构体类型变量* /
struct stu student3,student4;
用此结构体类型,可以定义更多的该结构体类型变量。

2 ) 定义结构体类型同时定义结构体类型变量:
struct data
{
     int day;
     int month;
     int year;
} time1,time2;
也可以再定义如下变量:
struct data time3,time4;
用此结构体类型,同样可以定义更多的该结构体类型变量。

3) 直接定义结构体类型变量:
struct
{
     char name[20]; / *学生姓名* /
     char sex; / *性别* /
     long num; / *学号* /
     float score[3]; / *三科考试成绩* /
} person1,person2; / *定义该结构体类型变量* /
该定义方法由于无法记录该结构体类型,所以除直接定义外,不能再定义该结构体类型变量。

4)

在C中定义一个结构体类型要用typedef:
typedef struct Student
{
int a;
}Stu;
于是在声明变量的时候就可:Stu stu1;
如果没有typedef就必须用struct Student stu1;来声明
这里的Stu实际上就是struct Student的别名。
另外这里也可以不写Student(于是也不能struct Student stu1;了)
typedef struct
{
int a;
}Stu;

时间: 2024-08-02 06:58:15

c 语言结构体struct的三种定义方式 及 typedef的相关文章

C语言结构体初始化的三种方法

直接上示例了 #include <stdio.h> struct student_st { char c; int score; const char *name; }; static void show_student(struct student_st *stu) { printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name); } int main(void) {

结构体数组初始化三种方法,转载

C语言结构体初始化的三种方法:原文链接http://www.2cto.com/kf/201503/386575.html 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> struct student_st {     char c;     int score;     const c

C语言 - 结构体(struct)的位字段(:) 详解

结构体(struct)的位字段(:) 详解 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26722511 结构体(struct)可以使用位字段(:), 节省空间, 如以下代码, 结构体a中的, 第一个变量x占用1个字符, y占用2个字符, z占用33个字符(越界); 但是sizeof()会自动补齐, 如x+y一共占用4个字节, z占用8个字节, 所以结构体占用12个字节; 当使用加法运算时, 会初始化为0; 代码: /* *

1、打印二进制机器码,程序内存分析,大端序小端序,指针数组,数组指针,数组的三种访问方式,typedef,#if-0-#endif,求数组大小,括号表达式

 1.打印二进制机器码(分别表示32位的和64位的) #include <stdio.h> /*按照8位的长度打印一个数值*/ void dis8bit(char val) { int bit = 8; while(bit--) { if(1<<bit&val){ printf("1"); } else { printf("0"); } if(!(bit%4)) printf(" "); } putchar(1

数组的三种定义方式

// 数组的三种定义方式 var arr = ['zhangsan','lisi','wangwu']; var arr1 = new Array('zhangsan','lisi','wangwu'); var arr2 = new Array(3); arr2[0] = 'zhangsan'; //注意:下标一定要写,不像在php中,下标是可以不写,是一直追加 // console.log(arr2); // 说明:js中,没有关联数组一说,数组下标都是数字 // 如果一个数组的下标是自定义

C语言结构体初始化的四种方法

定义 struct InitMember{    int first:    double second:    char* third:    float four;}; 方法一:定义时赋值 struct InitMember test = {-10,3.141590,"method one",0.25}: 需要注意对应的顺序,不能错位.方法二:定义后逐个赋值 struct InitMember test: test.first = -10;test.second = 3.14159

漫谈C语言结构体struct、公用体union空间占用

先用代码说话: #include<stdio.h> union union_data0{ int a ;//本身占用4个字节 char b ;//本身占用1个字节 int c ; }; union union_data1{ short a;//本身占用2个字节 char b[13];//本身占用13个字节 int c ;//本身占用4个字节 }; struct struct_data{ int a ;//int本身占用4个字节,偏移量为0 char b ;//char本身占用1个字节,偏移量为

Go语言结构体(struct)

Go 语言结构体 Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比如保存图书馆的书籍记录,每本书有以下属性: title       :书名 author   :作者 address       :地址 mobile         :手机号 publisher     :出版社 定义结构体 结构体定义需要使用 type 和 struct 语句.struct 语句定义一个

C语言结构体-struct

知识点: 1)结构体的定义. 2)结构体的sizeof. 3)  结构体的指针. 1) 结构体的定义: 在逻辑上有一定关联的多个数据类型做为一整体进行操作的数据结构,它的关键字是struct.下面我将定义一个结构体 struct Student{ char *name; int age; int sid; }; 我用上面定义的结构体Student来定义一个变量. struct Student student; 上面的代码有点烦,其实我可以这样写 struct Student{ char *nam