1.结构类型定义

结构类型将不同的数据类型组合起来,结构类型定义并没有说明任何实际的变量,它仅仅是定义一种特殊的数据类型;
结构类型定义本身为一条语句,以分号终止;
定义的一般形式:
            struct  结构名
            {
               类型成员 1;
               类型成员 2;
                .....
            } ;        ----------->这里有个分号,一定不能少。
 例如:   struct  person {
             char name[20];
             char address[30];
             int phone;
             } ;

2.结构类型变量的定义

共有三种方式:
(1)先定义结构类型再定义变量名
     比如: struct  person  pers1 , pers2;   若在程序开头预定义一个符号常量代表一个结构类型,
     即:    #define PERSON  struct person  (后面没有分号)
     则PERSON  与struct person 完全等效。可以定义结构变量 : PERSON  per1, per2;也可以定义结构类型。
(2)在定义类型的同时定义变量,这是一种双重定义:
    struct  score
   {
    char  grade[20];
    long int number;
    char  sex;
    float  database;
   } stu1, stu2; ------------->定义结构类型同时定义了两个结构变量stu1,stu2。
(3)可以将结构名省略:
   struct 
{
  ......
}结构变量名表;

3.结构变量的引用

对结构变量中各个成员的访问,用操作符“.”表示,具有最高优先级,其格式:
结构变量名 . 成员名,如: per1.number = 12354;

4.结构类型数组

一个结构变量只能存放表格中的一个记录,若需要很多的时候,可以定义结构类型数组;
例如: struct person stu[35] ;初始化 stu[2] = {{“aaa”,“bbb”,......},{"dfdf",.......},.......} ;

5.结构类型指针

一个结构变量的指针指向该变量所占用的内存段的起始地址,该指针变量的值是结构变量的起始地址。
定义: struct  结构名  *p;切记随后一定要将定义的指针指向结构类型变量,p = &结构变量;
通过p引用结构变量成员的形式:(*指针变量). 成员  或  指针变量 ->成员  ;

6.例子

(1)创建一个链表
代码:
#include <stdio.h>
#include <stdlib.h>
//定义结构体
struct node {int num;struct node *next;
};int main(){struct node *creat(struct node *head);void print(struct node *head);struct node *head;head = NULL;head = creat(head);print(head);return 0;
}
//创建链表,插入变量,返回一个头结点的首地址
struct node *creat(struct node *head){int n;struct node *p1 , *p2;printf("please input a number, if the number <0 then exit :");scanf("%d",&n);while (n>0){p1 = (struct node*)malloc(sizeof(struct node));p1->num = n;p1->next = NULL;if (head == NULL)head = p1;elsep2->next = p1;p2 = p1;scanf("%d",&n);}return head;
}
//输出函数
void print(struct node *head){struct node *temp;temp = head;while (temp != NULL){printf("%d ", temp->num);temp = temp->next;}
}
(2)以学生结构体为例,实现链表的创建,元素插入和删除。
-------链表中的创建、插入、删除,以学生学号为例---------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {int num;char name[20];struct node *next;
};
//------------主函数---------------
int main(){//------函数声明---------struct node *creat(struct node *head);struct node *insert(struct node *head ,struct node *insertnode);struct node *Delete(struct node *head , char *pstr) ;void print(struct node *head);struct node *head;head = NULL;char str[20];int num;//------创建链表----------head = creat(head);print(head);//------插入链表----------struct node *insp1;insp1 = (struct node *)malloc(sizeof(struct node));printf("please input inserted num ,name:\n");scanf("%d%s", &num, insp1->name );insp1->num = num;insp1->next = NULL;head = insert(head, insp1);print(head);//-------删除某节点------printf("please input delete student name:\n");scanf("%s", str);head = Delete(head, str);print(head);return 0;
}
//---------创建链表-----------
struct node *creat(struct node *head){char strname[20];int num;struct node *p1, *p2;//---申请节点空间,返回首地址。p1 = p2 =(struct node *) malloc(sizeof(struct node));printf("please input num , name :\n");scanf("%d%s", &num, p1->name );p1->num = num;p1->next = NULL;while (num > 0){if(head == NULL)head = p1;else p2->next = p1;p2 = p1;p1 =(struct node *) malloc(sizeof(struct node));printf("input next student num ,name:\n");scanf("%d%s", &num, p1->name );p1->num = num;p1->next = NULL;}return head;
}
//---------插入函数,保持学生学号升序----------------
struct node *insert(struct node *head ,struct node *insertnode){struct node *pre, *scanner;scanner = head;if (head == NULL){head = insertnode;insertnode->next = NULL;} else     //链表不空的情况{//---找到插入位置---while (insertnode->num > scanner->num && scanner->next != NULL){pre = scanner;scanner = scanner->next;}//-----判断该位置是不是在表尾-------if (insertnode->num <= scanner->num){//-----插入位置在表头------if (head == scanner){insertnode->next = scanner;head = insertnode;} else //------位置在表中--------{insertnode->next = scanner;pre->next = insertnode;}} //------插入位置在表尾--------else{scanner->next = insertnode;insertnode->next = NULL;}}return head;
}
//----------删除链表节点------
struct node *Delete(struct node *head, char *pstr){struct node *temp , *p;temp = head;if(head == NULL)return head;while (strcmp(temp->name , pstr)!= 0 && temp->next != NULL){p = temp;temp = temp->next;}if (strcmp(temp->name, pstr) == 0){if (temp == head){head = head->next;free(temp);}else{p->next = temp->next;printf("already delete %s \n", temp->name);free(temp);}} else{printf("can't find this  student !!\n");}return head;
}
//----------输出函数-----------
void print(struct node *head){struct node *temp;temp = head; while (temp != NULL){printf("%d  ,  %s\n", temp->num, temp->name);temp = temp->next;}
}

最后声明:本人以学习为目的,希望通过交流获取更多的知识,如有错误,还请大家批评指正,积极交流!!!

结构类型的定义,应用相关推荐

  1. opencv中图像的基本结构 类型的定义

    CvPoint 二维坐标系下的点,类型为整型 typedef struct CvPoint{int x; /* X坐标, 通常以0为基点 */int y; /* y坐标, 通常以0为基点 */} Cv ...

  2. c语言 结构类型(详细)

    前言 在编程中,有许多变量可以归为一类,例如 year,month,day三者可以归为date.于是乎就有了c语言的一个关键字:结构. 结构类型 结构类型是定义的新变量类型(和int,float地位一 ...

  3. 【C语言】结构体类型的定义与使用

    目录 一.结构体 二.结构体的定义 1.最基本的定义 2.有 typedef 的定义 3.定义即对象方式 4.定义即对象指针方式(涉及指针就会稍微复杂一点) 5. typedef结构体和结构体指针方式 ...

  4. 结构体变量和结构体类型的定义

    结构体类型定义 定义方式1:Typedef struct LNode {int data; // 数据域struct LNode *next; // 指针域 } *LinkList; 定义方式2:st ...

  5. c语言中结构体类型定义的函数指针,结构体中定义函数指针

    结构体指针变量的定义 定义结构体变量的一般形式如下:形式1:先定义结构体类型,再定义变量struct 结构体标识符{ 成员变量列表;-};struct 结构体标识符 *指针变量名;变量初始化:stru ...

  6. 【C 语言】结构体 ( 结构体类型定义 | 结构体类型别名 | 声明结构体变量的三种方法 | 栈内存中声明结构体变量 | 定义隐式结构体时声明变量 | 定义普通结构体时声明变量 )

    文章目录 一.结构体类型定义 二.结构体类型别名 三.结构体类型变量声明 1.使用结构体类型 ( 别名 ) 声明变量 2. 定义隐式结构体时声明变量 3.定义普通结构体时声明变量 二.完整代码示例 一 ...

  7. 结构体的类型,定义,引用

    结构体类型是有一系列相同类型或不同类型的数据构成的数据集合,结构体中的数据在逻辑上相互关联. 定义结构体类型的一般语句形式如下: struct 结构体类型名 { 数据类型 成员名1: 数据类型 成员名 ...

  8. 结构体中定义函数指针

    结构体指针变量的定义,定义结构体变量的一般形式如下: 形式1:先定义结构体类型,再定义变量 struct结构体标识符 { 成员变量列表;- }; struct 结构体标识符 *指针变量名; 变量初始化 ...

  9. c++结构体定义和使用_【C语言更新】结构体的定义及使用

    文/Edward首先先思考一个问题,假设某一天你去了一家策划公司,接到了一个策划需求,比如为新上市的某款手机写一个市场推广的文案,并且在电脑上面打印出来.那么在写这个文案的时候,你肯定是会需要着重地去 ...

最新文章

  1. Vue 生命周期记录_学习笔记
  2. Spring Boot Web 开发相关总结
  3. flask_sqlalchemy 中 or 、 and 和 like 的用法
  4. LetCode 70. Climbing Stairs--动态规划-爬梯子--递归等解法
  5. 安卓--L2T虚拟连接
  6. php try catch
  7. LeetCode MySQL 1501. 可以放心投资的国家
  8. 等保2.0丨2021 必须了解的40个问题
  9. 设计模式与设计原则 —— 一句话
  10. 面向对象的一些补充(type创建类,__mro__)
  11. 一线算法工程师总结:python常用数据挖掘算法PDF版
  12. 使用shell命令文件和lame工具在mac平台批量压缩mp3,可压缩至原来的50%左右。
  13. 计算机配色在纺织中的应用,计算机配色在印染行业的应用
  14. 中国公开课《如何培养孩子的学习兴趣》2020 郑日昌 观后感
  15. 侵害个人信息被工信部点名仍未整改,桔子分期产品被下架,盛银消金和金美信消金为合作方
  16. 电力系统中新型预测双二元变量机组组合问题(Matlab代码实现)
  17. s5p6818PWM驱动蜂鸣器实验
  18. 十三.人脸检测和车牌识别
  19. 使用计算机处理问题的步骤,1.1计算机解决问题的过程 计算机解决问题的5个步骤...
  20. 搭建turnserver(转) 稍加整理

热门文章

  1. Tecplot —— 探针提取数据集任一点数值
  2. 七,springBoot-SpringBootApplication注解
  3. rk3288 MINILOADERALL生成
  4. 计算机win7安装打印机,win7如何安装打印机驱动程序 win7系统安装打印机的方法...
  5. ⽬标⾏动及稠密环境未知情况下,⽆⼈机跟踪的系统解决⽅案
  6. ABB机器人线速度_ABB机器人设置程序开机自启动
  7. 域名过期后能否抢注过期高外链域名?
  8. react 使用 svg_在React本机中使用svg构建钟面
  9. 内网渗透之CFS三层靶场渗透
  10. 【JSD2209-DAY02】数据基本类型