C语言学习笔记(十二) -结构体 Struct
来源:互联网 发布:淘宝卖家 延长收货 编辑:程序博客网 时间:2024/05/01 07:48
定义结构体变量
/* 结构体 自定义的一种类型称为构造类型,在C语言中称为结构体 定义结构体: struct [结构体名] { [成员列表;] }; 定义结构体变量 1.struct 结构体名 变量名; { } 引用结构体成员 (运算符.) 结构体变量.成员 定义结构体变量并初始化 struct 结构体名 变量名={成员1初始化,成员2初始化....}; 结构体与数组类似,定义之后不能直接整体赋值,不能直接整体引用 */#include <stdio.h>#include <string.h>int main(void){ struct student { char name[20]; //instance variables int age; char sex[10]; float score[4]; }; //结构体类型定义结束 //定义结构体变量 struct student tom; // strcpy(tom.name,"tom"); // printf("姓名: %s\n",tom.name); //定义结构体变量并初始化 //struct student jack={"jack",32,{56,67,78}}; printf("输入姓名:\n"); gets(tom.name); printf("输入性别:\n"); gets(tom.sex); printf("输入年龄:\n"); scanf("%d",&tom.age); printf("输入分数:\n"); for (int i=0; i<4; i++) { scanf("%f",&tom.score[i]); } printf("姓名: %s\n",tom.name); printf("性别: %s\n",tom.sex); printf("年龄: %d\n",tom.age); //遍历输出 for (int i=0; i<4; i++) { printf("分数: %f\n",tom.score[i]); } return 0;}
/* 1.定义结构体的同时定义结构体变量 struct [结构体名] { }变量名[={初始化值}]; 2.定义省略结构体 省略结构体名定义类型时必须在定义时指定所需的所有变量 struct { }name1,name2,name3[4]; */#include <stdio.h>int main(void){ //定义结构体的同时定义结构体变量并赋初值 struct birthday { int year; int month; int day; }Bri={1990,10,4}; printf("出生日期为: %d %d %d \n",Bri.year,Bri.month,Bri.day); return 0;}
/* 在结构体类型中使用其他的结构体类型定义结构体变量,需要先定义内部结构体变量的类型方可使用 */#include <stdio.h>struct birthday { int year; int month; int day;};struct student { char name[20]; struct birthday bir; float score[4];};int main(void){ //结构体的初始化 struct student Alice={"Alice",{1990,10,4},{7,8,9,10}}; printf("%s: %d-%d-%d\n",Alice.name,Alice.bir.year,Alice.bir.month,Alice.bir.day); int len =sizeof(Alice.score)/sizeof(Alice.score[0]); for (int i=0; i<len; i++) { printf("%f ",Alice.score[i]); } printf("\n"); return 0;}
结构体数组
/* 结构体数组 */#include <stdio.h>struct birthday { int year; int month; int day;};struct student { char name[20]; struct birthday bir; float score[4];};int main(void){ //定义结构体数组 struct student arr[3]; int len =sizeof(arr)/sizeof(arr[0]); for (int i=0; i<len; i++) { scanf("%s:",arr[i].name); scanf("%d-%d-%d",&arr[i].bir.year, &arr[i].bir.month, &arr[i].bir.day); scanf("%f %f %f %f",&arr[i].score[0], &arr[i].score[1], &arr[i].score[2], &arr[i].score[3]); } printf("输出结果为:\n"); for (int i=0; i<len; i++) { printf("%s ",arr[i].name); printf("%d-%d-%d ",arr[i].bir.year, arr[i].bir.month, arr[i].bir.day); printf("%f %f %f %f ",arr[i].score[0], arr[i].score[1], arr[i].score[2], arr[i].score[3]); printf("\n"); } printf("\n"); return 0;}
/* 结构体变量之间可以直接赋值 *//*#include <stdio.h>struct student{ char name[20]; int i;};int main(void){ struct student a={"hello",12}; struct student b; b=a; printf("b.name=%s b.i=%d\n",b.name,b.i); return 0;}*///交换结构体内容并输出#include <stdio.h>struct student{ char name[20]; int age;};int main(void){ struct student temp; struct student a={"hello tom",22}; struct student b={"hello jack",25}; temp=a; a=b; b=temp; printf("b.name=%s b.age=%d\n",b.name,b.age); printf("a.name=%s a.age=%d\n",a.name,a.age); //结构体数组排序原理 /* for (int i=0; i<len-1; i++) { min=i; for (int j=i+1; j<len; j++) { if (strcmp(arr[min].name,arr[j].name)>0) { min=j; } } t=arr[min]; arr[min]=arr[i]; arr[i]=t; } */ return 0;}
结构体做函数参数,值是单项传递的
/* 结构体做函数参数,值是单项传递的 */#include <stdio.h>#include <string.h>struct student{ char name[20]; int age;};void output(struct student);int main(void){ struct student a={"xioaming",25}; output(a); printf("a:%s %d\n",a.name,a.age); return 0;}void output(struct student m){ printf("m:%s %d\n",m.name,m.age); strcpy(m.name,"xiaohua");}
指向结构体变量的指针变量
//指向结构体变量的指针变量 struct type * //指针变量引用结构体成员运算符 -> //引用成员的三种方法 1.a.i 2. p->i 3.(*p).i#include <stdio.h>struct type{ int i;};void input(struct type *);void output(struct type *arr,int len);int main(void){ struct type a; input(&a); printf("a.i=%d\n",a.i); //数组结构体 struct type arr[10]={{12},{22},{13},{34},{25},69,47,83,79,40}; output(arr,10); return 0;}void input(struct type *p) //p=&a{ // p->i =12; //a.i (*p).i=33;}void output(struct type *arr,int len)//struct type arr[]{ for (int i=0; i<len; i++) { printf("arr[%d]=%d\n",i,arr->i);//arr[i].i arr++; }}
练习:使用回调函数实现排序函数,使之能够处理任何类型的数组
//使用回调函数实现排序函数,使之能够处理任何类型的数组//排序结构体数组#include<stdio.h>typedef void SORT(void*,int,void(*)(void*,int,int),int(*)(void *,int,int));typedef void (*S)(void*,int,int);void sort(void*,int,void(*swap)(void*,int,int),int(*cmp)(void*,int,int));void swap(void*,int ,int);int cmp( void *,int,int);struct aa{ int i; char x;};void output(struct aa *,int);int main(){ struct aa a[5] = {{34,'a'},{53,'b'},{199,'c'},{32,'d'},{58,'e'}}; output(a,5); // struct aa *p = a; sort(a,5,swap,cmp); output(a,5); printf("a[0] = %d\n",a[0].i); return 0;}int cmp( void *a,int min,int j){ struct aa *p = (struct aa *)a; return (p+min)->i - (p+j)->i;}void swap(void *a,int i,int j){ struct aa * p1 = (struct aa *)a; struct aa t; t = *(p1+i); *(p1+i) = *(p1+j); *(p1+j) = t;}void sort(void *a,int num,void (*swap)(void*,int,int),int (*cmp)(void *,int,int )){ int min=0;//下标 for(int i=0;i<num-1;i++) { min =i; for(int j=i+1;j<num;j++) { if(cmp(a,min,j)>0) min = j; } swap(a,min,i); }}void output(struct aa *a,int num){ for(int i=0;i<num;i++) { printf("%d- %c\n ",a->i,a->x); a++; } printf("输出结束.\t");}
0 0
- C语言学习笔记(十二) -结构体 Struct
- C++学习笔记二十二 - 简单的结构体struct
- C语言学习之结构体struct
- C语言结构体(struct)
- c语言结构体struct
- C语言结构体struct
- c语言struct结构体
- c 语言学习笔记十二
- 【黑马程序员】C语言学习笔记之结构体(十二)
- 学习日记-C语言结构体(struct)
- [struct]C语言编程结构体的学习
- C语言学习-struct结构体定义你自己的数据类型
- ⑤NDK学习之C语言结构体struct
- ⑥NDK学习之C语言结构体struct例子
- C结构体struct学习
- Objective-C学习笔记-之结构体struct
- Struct 结构体疑惑(c语言)
- C语言中的struct option结构体
- STM32F103 DS18B20 V3.5.0固件库驱动程序
- iOS8开发~UI布局(二)storyboard中autolayout和size class的使用详解
- 彩票案例-封装知识点
- 在一个字符串中找到第一个只出现一次的字符
- myeclipse10.0优化
- C语言学习笔记(十二) -结构体 Struct
- JMS,ActiveMQ_基础学习 GOOD
- 彩票案例-UIWebView学习
- 焊盘的层面剖析
- python 安装wheel .whl文件
- 【基础】linux系统-->启动过程
- Max Sum (hdu 1003 简单DP水过)
- 彩票案例-知识点补充
- 【干货】细说Unity3D——移动平台动态读取外部文件全解析