(c基础)上课笔记 12.20

  • 结构体

基本定义:
struct 结构名
{
//成员列表
};
成员列表:有基本数据类型定义的变量或者是构造类型的变量
example:
struct student
{
int grade;
int age;
char name[32];
};
student :结构体名称
----结构体变量可以直接赋值给相同类型的结构体变量。
struct student :结构数据类型,相当于int,double,char等基本数据类型;
struct student stu; stu为结构体变量
访问结构体成员:
stu.name;
stu.grade;
stu.age;

  • 结构体的初始化1

struct student
{
char name[32]
char sex;
int age;
};
struct student boy;
strcpy(boy.name,"jeck");
boy.age=24;
boy.sex='m';

  • 初始化2

struct student girl={"jecl"}

#include <stdio.h>
#include <string.h>
struct student 
{
    char name[32];
    char sex;
    int age;
};
int main()
{
    struct student stu;
    strcpy(stu.name,"lisan");
    stu.sex='m';
    stu.age=24;//结构体的初始化
    printf("name %s,sex %c,age %d\n",stu.name,stu.sex,stu.age);
    printf("方法二\n");
    //初始化必须要和成员列表一一对应。
    struct student stu1={"lily",'f',20};//结构体的初始化
    printf("name %s,sex %c,age %d\n",stu1.name,stu1.sex,stu1.age);
    return 0;
}
  • 初始化3(不建议):定义的同时初始化

#include <stdio.h>
#include <string.h>
struct student 
{
    char name[32];
    char sex;
    int age;
}stu,stu1,stu2;//stu={"xiaoming",'m',23};
  • 无名结构体

(一般不使用)

#include <stdio.h>
#include <string.h>
struct
{
    char name[32];
    int age;
}stu;
  • 宏定义结构体

#include <stdio.h>
#include <string.h>
struct student
{
    char name[32];
    int age;
};
#define STU struct student
STU stu;
  • 结构体的嵌套

#include <stdio.h>
#include <string.h>
struct date
{
    int year;
    int month;
    int day;
};
struct sutdent
{
    char name[32];
    int age;
    struct date birthday;
};
int main()
{
    struct student sut;
    strcpy(stu.name,"hello")'\;
    stu.age=34;
    stu.birthday.year=1900;
    stu.birthday.month=5;
    stu.birthday.day=6;
    printf("%s,%d,%d,%d,%d\n",stu.name,sut.birthday.year,sut.birthday.month,stu.birthday.day);
    return 0;
}
  • 嵌套定义结构体

struct sutdent
{
    int a;
    char b;
    struct student stu;//无法确定大小,不能分配空间
};
struct sutdent1
{
    int a;
    char b;
    struct student1 *ps;//所以的指针类型大小都为8个字节
};
  • 结构体数组

#include <stdio.h>
#include <string.h>
struct sutdent
{
        int age;
    char name[32];
};
int main()
{
    struct student stu[3]=
    {
     {24,"hello"},
     {20,"hello"},
     {26,"hello"},
    };
    //结构体数组的访问
    printf("stu[1]=%d,stu[1]=%s.\n",stu[1].age,stu[1].name);
    return 0;
}
  • 结构体指针

#include <stdio.h>
#include <string.h>
struct date
{
    int year;
    int month;
    int day;
};
struct sutdent
{
    char name[32];
    int age;
    struct date birthday;
};
void main()
{
   struct sutdent stu;
   struct sutdent *p=&stu;//空指针不能访问其值
   strcpy(p->name,"hello");
   p->age=13;
   p->birthday.year=1990;
   p->birthday.month=2;
   p->birthday.day=13;
   printf("%s,%d,%d,%d,%d\n",p->name,p->age,p->birthday.year,p->birthday.month,p->birthday.day);
}
  • 堆栈

#include <stdio.h>
#include <stdlib.h>
malloc;//申请堆空间
free;//释放空间
pa=(struct date *)malloc(sizeof(struct date));//申请一个大小为sizeof(struct date)的堆空间。
free(pa);//释放申请的堆空间
  • typedef

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   typedef int I;//相当于把int重新定义了名称为I.
   I a=4;
   int b=5;
   I c=a+b;
   printf("a=%d,b=%d,c=%d.",a,b,c);
   return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct sutdent
{
   int age;
   char name[23];
}STU;
/*
struct sutdent
{
   int age;
   char name[23];
};
typedef struct sutdent STU;//这样定义是一样的
*/
int main()
{
   STU stu;
   strcpy(stu.name,"wang");
   stu.age=34;
   printf("name=%s,age=%d.\n",stu.name,stu.age);
   return 0;
}

---- 和宏定义的区别
typedef struct student STU;
#define STU struct student

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sutdent
{
   int age;
   char name[23];
}STU;
typedef struct student* STT;
#define STD struct student*
int main()
{
   STT stu1,stu2;//stu1和stu2都是结构体指针
   STD stu3,stu4;//struct student *stu3,stu4  stu3是指针,stu4是变量
   printf("name=%s,age=%d.\n",stu.name,stu.age);
   return 0;
}
  • 结构体大小

内存对其:
Linux: 4字节
Windows: 8字节
默认从偏移量为0的位置开始储存,每个成员

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct A    //8
{           //[0-3]
    char a; //[0]
    int b;  //[4-7]
};
struct B    //12
{
    char a; //[0]
    int c;  //[4-7]
    char b; //[8]
};
struct C
{
    int a;
    char b;
};
struct D    //8
{
    char a; //[0]
    char b; //[1]
    int c; //[4-7]
};
struct E   //8
{
    char a; //[0]
    int c; //[4-7]
    char b[23]; //[1]
};
struct F    //6
{
    char a; //[0]
    short c //[2-3]
    char b; //[4]
};
struct G    //8
{
    char a; //[0]
    char b; //[1]
    short c; //[2-3]
};
int main()
{
   printf("A:%ld\n",sizeof(struct A));
   printf("B:%ld\n",sizeof(struct B));
   printf("C:%ld\n",sizeof(struct C));
   printf("D:%ld\n",sizeof(struct D));
}
  • 联合体union untype

{
int a;
long b;
int arr[4];//联合体的内存大小以最大的为准
};
特点,每次只能操作一个成员变量。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
union untype
{
   int a;
   double b;
   char c;
};//最多只能操作一个变量
int main()
{
   union untype un;
   un.a=123;
   un.b=3.14;
   un.c='f';
   printf("a=%d,b=%lf,c=%c.\n",un.a,un.b,un.c);//此次错误,不管是赋值还是输出同一时间只能访问一个值。
   return 0;
}
  • 枚举类型

#include <stdio.h>
enum entype//成员一般用大写
{
   A,   //0
   B,   //1
   C=12,
   D,   //13
   E    //14 
};
int main()
{
   printf("A=%d\n",A);
   printf("B=%d\n",B);
   printf("C=%d\n",C);
   printf("D=%d\n",D);
   printf("E=%d\n",E);
   return 0;
}

----与switch的连用

#include <stdio.h>
enum entype//成员一般用大写
{
   FIRST=1,   //0
   SEC,   //1
   THIRD
};
int main()
{
   int op=0;
   printf("input op:");
   scanf("%d",&op);
   switch(op)
   {
      case FIRST:printf("第一名!\n");break;
      case SEC:printf("第二名!\n");break;
      case THIRD:printf("第三名!\n");break;
      default :printf("谢谢参与!\n");break;
   }
   return 0;
}

----定义enum型变量

#include <stdio.h>
enum entype//成员一般用大写
{
   A,
   B,
   C
};
int main()
{
   enum type en;//定义变量
   en=A;
   return 0;
}
  • 链表

----链式存储结构,线性存储结构,其大小可动态改变,链表是由一个个节点串起来的数据链。
----节点:由数据域和指针域组成。
----数据域:存放数据。
----指针域:存放下一个节点的地址

  • 创建链表

#include <stdio.h>
struct student
{
    int id;
    struct student *next;
}
struct student *head;

malloc():申请空间
free():关闭空间
创建一个头节点:

struct student *head;
head=(struct student *)/*强制类型转换*/malloc(sizeof(struct student));

头节点标识一个链表,即链表名称。
头节点的数据域不存放数据,指针域存放

#include <stdio.h>
#include <stdlib.h>//malloc();,free();
struct student 
{
   int ID;
   char name[32];
   struct student *next;
};
#define LEN sizeof(struct student)
struct student *add_link(struct student *head)
{
  struct student *temp=(struct student *)malloc(LEN);
  printf("input ID:");
  scanf("%d",&temp->ID);
  printf("input name:");
  scanf("%s",temp->name);
  temp->next=head->next;
  head->next=temp;
  temp=NULL;
  return head;
}
void show_link(struct student *head)
{
   struct student *p=head->next;
   while(p!=NULL)
   {
    printf("\t%d\t%s\n",p->ID,p->name);
    p=p->next;
   }
}
int main()
{
   struct student *head;//创建链表
   head=(struct student *)malloc(LEN);
   head->next=NULL;//需要将next指针置空,防止野指针
   head=add_link(head);
   show_link(head);
   return 0;
}
  • 作业

1.尾增

#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
    int id;
    char name[32];
    int num;
    struct student *next;
}STU,*pSTU;

pSTU add(pSTU head)
{
    pSTU temp=(pSTU)malloc(sizeof(STU));
    printf("in put ID:");
    scanf("%d",&temp->id);
    printf("in put name:");
    scanf("%s",temp->name);
    printf("in put num:");
    scanf("%d",&temp->num);
    temp->next=head->next;
    head->next=temp;
    temp=NULL;
    return head;
}
void show_add(pSTU head)
{
    pSTU p=head->next;
    while(1)
    {
    if(p!=NULL)
    {
       printf("\t%d\t%s\t%d\n",p->id,p->name,p->num);
       p=p->next;
    }
       else break;
    }
}
void show_del(pSTU head)
{
    pSTU p=head->next;
    head->next=p->next;
    free(p);
    p=NULL;
}
void main()
{
    int i,n;
    pSTU head=(pSTU)malloc(sizeof(STU));
    head->next=NULL;
    for(i=0;i<3;i++)
    head=add(head);
    show_add(head);
    printf("\n");
    show_del(head);
    show_add(head);
}

2.尾减

#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
   int ID;
   char name[10];
   struct student *next;
}STU,*pSTU;
pSTU add_STU(pSTU head)
{
    pSTU temp=(pSTU)malloc(sizeof(STU)),p=head;
    temp->next=NULL;
    printf("in put(ID,name) :");
    scanf("%d,%s",&temp->ID,temp->name);
    while(p->next!= NULL)
    {
     p=p->next;
    }
     p->next=temp;
     temp=NULL;
    return head;
}
void show_del_STU(pSTU head)
{
  pSTU p=head,p1=head->next->next;
  if(head->next!=NULL)
  {
  while(p1!=NULL)
  {
  p=p->next;
  p1=p1->next;
  }
  free(p->next);
  p->next=NULL;
  }
}
void show_add_STU(pSTU head)
{
    pSTU p=head->next;
    while(1)
    {
       if(p!=NULL)
       {
       printf("%5d,%s\n",p->ID,p->name);
       p=p->next;
       }
       else break;
    }
}
void main()
{
    pSTU head=(pSTU)malloc(sizeof(STU));
    head->next=NULL;
    int i;
    for(i=0;i<5;i++)
    head=add_STU(head);
    show_add_STU(head);
    printf("\n");
    show_del_STU(head);
    show_add_STU(head);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容