c语言指针变量输出不了共用体,瘋子C语言札记(结构体/共用体/枚举篇)

瘋子C语言笔记(结构体/共用体/枚举篇)

(一)结构体类型

1.简介:

例:

struct date

{

int month;

int day;

int year;

};

struct student

{

int num;

char name[20];

char sex;

int age;

struct date birthday;  /*birthday 是 struct date 类型*/

char addr[30];

}student1,student2;

(1):结构体可嵌套

(2):与枚举相比结构体内元素为变量,而枚举为常量

(3):元素只能单个引用:如:student.num;  student.birthday.month;

(4):内部元素变量可以与其他变量重名且互不影响;

(5):赋值:student.num = 10010;

2.初始化

(可直接赋值)

struct student

{

long int num;

char name[20];

char sex;

char addr[20];

}a = {10101,"Li Lin","M","BeiJing Road"};

3.结构体数组

struct student

{

int num;

char name[20];

char sex;

int age;

struct date birthday;  /*birthday 是 struct date 类型*/

char addr[30];

};

struct student stu[3];

注:数组中每一个元素都是一个结构体类型数据;

4.指向结构体类型数据的指针

struct student

{

int num;

char name[20];

char sex;

int age;

struct date birthday;  /*birthday 是 struct date 类型*/

char addr[30];

};

struct student stu_1;

struct student *p;

p = &stu_1;

注:

(1)一个结构体变量的指针就是该变量所占据的内存段的起始地址(开头地址);

(2)(*p).num 等价于 stu_1.num;(*p两端的括号不能省略)

(3)(*p).num 等价于 p->num; (-> 称为指向运算符)

(4)如下运算

p->n; 得到p指向的结构体变量中的成员n的值。

p->n ++;得到p指向的结构体变量中的成员n的值,用完该值后使它加1。

++ p->n;得到p指向的结构体变量中的成员n的值加1,然后再使用它。

-> 运算优先级大于 ++;

5.指向结构体数组元素的指针

struct student

{

int num;

char name[20];

char sex;

int age;

struct date birthday;  /*birthday 是 struct date 类型*/

char addr[30];

};

struct student stu[3];

struct student *p;

for( p = stu;p < stu + 3;p ++)

printf( "%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age);

注:p是一个指向struct student 类型数据的指针变量,它用来指向一个struct student 类型的数据(及:stu数组的一个元素如stu[0],stu[1],的起始地址),不应用来指向stu数组元素中的某一成员,

p = stu[1].name;   这样的用法是错误的

但是:

强制类型转换后可以用

p = (struct student *)stu[0].name;  这样是正确的;

这样的话:p的值是stu[0]元素的name成员的起始地址。printf(“%s”,p)输出stu[0]中成员name的值。但printf(“%s”,p +1)则输出 stu[1]中name的值。p+1 的值是在p值的基础上加上结构体struct student的字节长度。

因为:p加减运算他的类型就是他的最小运算单元

6.结构体变量做函数参数

注:用结构体变量做函数参数时,采用值传递的方式,将结构体变量所占的内存单元的内容全部顺序传递给形参。

#include

#include

#define FORMAT "%d\n%s\n%f\n%f\n%d\n"

struct student

{

int num;

char name[20];

float score[3];

} ;

void main()

{

void print(struct student);

struct student stu;

stu.num = 12345;

strcpy(stu.name,"Li Li");

stu.score[0] = 67.5;

stu.score[1] = 89;

stu.score[2] = 78.6;

print(stu);

}

void print(struct student stu)

{

printf(FORMAT,stu.num,stu.name,stu.score[0], stu.score[1],stu.score[2]);

printf("\n");

}

运行结果:

12345

Li Li

67.500000

89.000000

78.599998

7.用指针处理链表

例:

#include

#define NULL 0

struct student

{

long num;

float score;

struct student *next;

};

void main()

{

struct student a,b,c,*head,*p;

a.num = 10101;a.score = 89.5;

c.num = 10103;c.score = 90;

c.num = 10107;c.score = 85;

head = &a;

a.next = &b;

b.next = &c;

c.next = NULL;

p = head;

do

{

printf("%ld%5.1f\n",p -> num,p -> score);

p = p -> next;    //使P指向下一结点

}

while(p != NULL);

}

运行结果:

10101  89.5

10103  90.0

10107  85.0

注:

该例子结点都是在程序中定义的,不是临时开辟的,也不能用完后释放,这种链表称为“静态链表”。

8.处理动态链表

①malloc函数

原型:void *malloc(unsigned int size)

其作用是在内存的动态存储区分配一个长度为size的连续空间。此函数的值(即“返回值”)是一个分配域的起始地址(类型为void)。如果此函数未能成功地执行(例如内存不足),则返回空指针(NULL)。

②calloc函数

原型:void *malloc(unsigned n unsigned int size);

其作用是在内存的动态存储区分配n个长度为size的连续空间。函数返回一个指向分配域起始位置的指针;如果分配不成功,返回NULL;

③free函数

原型:void free (void *p);

其作用是释放由p指向的动态存储区,使这部分内存区能被其他变量使用。p是最近一次调用calloc或malloc函数时返回的值。free函数无返回值。

9.建立动态链表

(二)共用体

1.概述:使几个不同的变量共占同一段内存的结构,称为“共用体”类型的结构;

2.定义

union 共用体名

{

成员表列;

}变量表列;

如:

union data

{

int i;

char ch;

float f;

}a,b,c;

或者

union data

{

int i;

char ch;

float f;

};

union data a,b,c;

注:共用体与结构体定义形式相似,但占内存不同,结构体占的是各个成员内存和,共用体的内存是成员中字节最长的那个;

3.共用体变量的引用方式

必须先定义后引用,引用时只能引用共用体变量中的成员。

如:

a.i

a.ch

a.f

4.共用体类型数据的特点

(1)同一个内存段可以用来存放几种不同类型的成员,但在每一瞬时只能存放其中一种,而不是同时存放几种。所以对于共用体变量中起作用的成员是最后一次存放的成员。

如:

a.i = 1;

a.c = 'a';

a.f = 1.5;

只有a.f 是有效的,a.i ,a.c已经无意义了,此时用"printf("%d",a);"是不行的,"printf("%d",a.i);"是不行的,"printf("%d",a.f);"可以;

(2)共用体变量的地址、它的各成员的地址都是同一地址。

即:&a, &a.i, &a.c, &a.f都是同一地址;

(3)不能对共用体变量名赋值,不能赋值给共用体名,不能定义共用体时对它初始化。

union data

{

int i;

char ch;

float f;

}a={1,'a',1.5};    错误

a = 1;        错误

m =a;        错误

(4)不能把共用体变量作为函数参数,也不能使函数带回共用体变量,但可以使用指向共用体变量的指针(和结构体变量这种用法相仿)。

(5)共用体类型可以出现在结构体类型定义中,也可以定义共用体数组。反之,结构体也可以出现在共用体类型定义中,数组也可以作为共用体的成员。

(三)枚举类型

1.enum weekday{sum,mon,tue,wed,thu,fri,sat};(枚举定义的量都为常量)

声明一个枚举类型 enum weekday,可以用此类型来定义变量。如:

enum weekday workday,week_end;

workday和week_end被定义为枚举变量,它们的值只能是sum到sat之一。如:

workday = mon;

week_end = sum;

等价于

enum {sum,mon,tue,wed,thu,fri,sat} workday,week_end;

它们都是其中之一。

2.当然可以用

typedef  enum {sum,mon,tue,wed,thu,fri,sat}  weekday;

即等价于 enum weekday{sum,mon,tue,wed,thu,fri,sat};

3.枚举类型特点

(1)枚举元素作为常量,它们是有值的,C语言编译按定义时的顺序使它们的值为 0,1,2.....

在上面定义中,sun的值为0,mon的值为1......sat的值为6。

例如:

printf("%d",workday);

将输出整数 1。

可以在定义式对元素赋初值

如:

enum weekday {sum =7,mon =1,tue,wed,thu,fri,sat}  workday,week_end;

定义sun为7,mon为1,其他值还是按顺序给值,即sat任为6。

③枚举类值可比较大小做判断。

(2)一个整数不能直接赋给一个枚举变量

如:

workday = 2;    错误

但可

workday = (eunm weekday)2;    正确

#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;i<6;i++) { scanf("%d",&temp2); p->score[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;i<6;i++) { scanf("%d",&score); q->score[i]=score; } q->next=NULL; p=head; while(p->next->num<q->num&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;i<6;i++) printf("\t%d",p->score[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;i<6;i++) { scanf("%d",&score[i]); p->score[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值