结构体变量的定义和初始化
定义结构体变量的方式:
先声明结构体类型再定义变量名
在声明类型的同时定义变量
直接定义结构体类型变量(无类型名)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//定义结构体格式
/*
struct 结构体名称
{
结构体成员列表
};
//定义结构体变量
struct 结构体名称 结构体变量名
结构体变量名.结构体成员列表 = 值
如果是字符串类型 需要使用strcpy
*/
struct students
{
//成员列表
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
}stu = { "尼古拉斯",500,"13888888888",100.0f,200,300,'M' };
int main02()
{
//按照结构体顺序 赋值
//struct students stu = { "尼古拉斯",500,"13888888888",100.0f,200,300,'M' };
//struct students stu = { .sex = 'M',.name = "刘能",.tel = "13777777777",.scores[0] = 100,.scores[1] = 99,.scores[2] = 88,.age = 48 };
//struct students stu;
stu.name = "谢广坤";//字符串不能这样赋值,只能拷贝的方式,name是一个数组,name就是指针,这样是修改指针的地址了
//strcpy(stu.name, "谢广坤");
//stu.age = 50;
//strcpy(stu.tel, "13777777778");
//stu.scores[0] = 90;
//stu.scores[1] = 80;
//stu.scores[2] = 70;
//stu.sex = 'F';
//strcpy(stu.name, "谢广坤");
//stu.age = 50;
//strcpy(stu.tel, "13777777778");
//stu.scores[0] = 90;
//stu.scores[1] = 80;
//stu.scores[2] = 70;
//stu.sex = 'F';
printf("姓名:%s\n", stu.name);
printf("年龄:%d\n", stu.age);
printf("电话: %s\n", stu.tel);
printf("成绩: %.1f %.1f %.1f\n", stu.scores[0], stu.scores[1], stu.scores[2]);
printf("性别: %s\n", stu.sex == 'M' ? "男" : "女");
system("pause");
return EXIT_SUCCESS;
}
结构体数组
struct stu
{
//成员列表
char name[21];
unsigned int age;
char tel[16];
float scores[3];//scores[0]
char sex;
};
int main04()
{
//结构体数组
struct stu s[2];
for (int i = 0; i < 2; i++)
{
printf("请您输入 姓名 年龄 电话 成绩 性别:\n");
scanf("%s%d%s%f%f%f,%c", s[i].name, &s[i].age, s[i].tel, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2], &s[i].sex);
}
for (int i = 0; i < 2; i++)
{
printf("姓名:%s\n", s[i].name);
printf("年龄:%d\n", s[i].age);
printf("电话: %s\n", s[i].tel);
printf("成绩: %.1f %.1f %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
printf("性别: %s\n", s[i].sex == 'M' ? "男" : "女");
}
system("pause");
return EXIT_SUCCESS;
}
结构体学生成绩排序
struct stu1
{
//成员列表
char name[21];
float scores[3];
};
int main()
{
struct stu1 s[3];
for (int i = 0; i < 3; i++)
{
printf("请您输入学生 姓名 成绩 :\n");
scanf("%s%f%f%f", s[i].name, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2]);
}
//冒泡排序
for (int i = 0; i < 3 - 1; i++)
{
for (int j = 0; j < 3 - i - 1; j++)
{
int sum1 = s[j].scores[0] + s[j].scores[1] + s[j].scores[2];
int sum2 = s[j + 1].scores[0] + s[j + 1].scores[1] + s[j + 1].scores[2];
if (sum1 > sum2)
{
//结构体交换 交换所有成员列表中的数据
//交换姓名
char temp[21] = { 0 };
strcpy(temp, s[j].name);
strcpy(s[j].name, s[j + 1].name);
strcpy(s[j + 1].name, temp);
//交换成绩
for (int k = 0; k < 3; k++)
{
float temp=s[j].scores[k];
s[j].scores[k] = s[j + 1].scores[k];
s[j + 1].scores[k] = temp;
}
}
}
}
for (int i = 0; i < 3; i++)
{
printf("姓名:%s\n", s[i].name);
printf("成绩: %.1f %.1f %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
}
system("pause");
return EXIT_SUCCESS;
}
结构体成员为指针
#include "iostream"
#include "string.h"
#include "stdlib.h"
using namespace std;
struct stbInfo
{
char *name;
int age;
};
int main()
{
struct stbInfo str;
str.name = (char *)malloc(sizeof(char)* 21);
strcpy(str.name, "张三");
str.age = 18;
cout << "name====" << str.name << endl;
cout << "age====" << str.age << endl;
free(str.name);
system("pause");
return EXIT_SUCCESS;
}
结构体指针
. | 成员选择(对象) | 对象.成员名 |
-> | 成员选择(指针) | 对象指针->成员名 |
#include "iostream"
#include "string.h"
#include "stdlib.h"
using namespace std;
struct stbInfo
{
char *name;
int age;
}stut;
int main()
{
struct stbInfo *s = & stut;
s->name = (char *)malloc(sizeof(char)* 21);
strcpy(s->name, "李芮");
s->age = 50;
cout << "name====" << s->name << endl;
cout << "age====" << s->age << endl;
free(s->name);
system("pause");
return EXIT_SUCCESS;
}
堆空间开辟结构体
#include "iostream"
#include "string.h"
#include "stdlib.h"
using namespace std;
struct stbInfo
{
char *name;//4
int age;//4
}t;
int main()
{
struct stbInfo *p = (struct stbInfo *) malloc(sizeof(t));
p->name = (char *)malloc(sizeof(char)* 21);
strcpy(p->name, "李芮");
p->age = 50;
cout << "name====" << p->name << endl;
cout << "age====" << p->age << endl;
if (p->name != NULL)
{
free(p->name);
p->name = NULL;
}
if (p)
{
free(p);
p = NULL;
}
system("pause");
return EXIT_SUCCESS;
}
学生成绩冒泡排序
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu2
{
//成员列表
//char name[21];
char * name;
float * scores;
};
int main()
{
struct stu2 *p = (struct stu2 *)malloc(sizeof(struct stu2) * 3);
for (int i = 0; i < 3; i++)
{
p[i].name = (char *)malloc(sizeof(char) * 21);
p[i].scores = (float *)malloc(sizeof(float) * 3);
printf("请您输入学生 姓名 成绩 :\n");
scanf("%s%f%f%f", p[i].name, &p[i].scores[0], &p[i].scores[1], &p[i].scores[2]);
}
//冒泡排序
for (int i = 0; i < 3 - 1; i++)
{
for (int j = 0; j < 3 - i - 1; j++)
{
float sum1 = p[j].scores[0] + p[j].scores[1] + p[j].scores[2];
float sum2 = p[j + 1].scores[0] + p[j + 1].scores[1] + p[j + 1].scores[2];
if (sum1 > sum2)
{
struct stu2 temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
for (int i = 0; i < 3; i++)
{
printf("姓名:%s\n", p[i].name);
printf("成绩: %.1f %.1f %.1f\n", p[i].scores[0], p[i].scores[1], p[i].scores[2]);
}
//释放
for (int i = 0; i < 3; i++)
{
free(p[i].name);
free(p[i].scores);
}
free(p);
system("pause");
return EXIT_SUCCESS;
}
函数的参数是机构体
#include "iostream"
using namespace std;
struct info
{
char name[21];
int age;
};
//值传递不会修改
void fun01(struct info s)
{
strcpy(s.name, "李四");
s.age = 20;
cout << "fun01==s.name====" << s.name << endl;
cout << "fun01==s.age====" << s.age << endl;
//fun01 == s.name == == 李四
//fun01 == s.age == == 20
}
void fun02(struct info *s)
{
strcpy(s->name, "李四");
s->age = 20;
}
int main()
{
struct info s = { "张三", 18 };
//值传递不会修改实参的值
//fun01(s);//main==s.name====张三 main == s.age == == 18
//指针传递修改了实参的值
fun02(&s); //main==s.name====李四 main == s.age == == 20
cout << "main==s.name====" << s.name << endl;
cout << "main==s.age====" << s.age << endl;
system("pause");
return EXIT_SUCCESS;
}
返回值是结构体
// 21结构体函数.cpp : 定义控制台应用程序的入口点。
//
#include "iostream"
using namespace std;
struct info
{
char name[21];
int age;
};
//值传递不会修改
void fun01(struct info s)
{
strcpy(s.name, "李四");
s.age = 20;
cout << "fun01==s.name====" << s.name << endl;
cout << "fun01==s.age====" << s.age << endl;
//fun01 == s.name == == 李四
//fun01 == s.age == == 20
}
void fun02(struct info *s)
{
strcpy(s->name, "李四");
s->age = 20;
}
int main01()
{
struct info s = { "张三", 18 };
//值传递不会修改实参的值
//fun01(s);//main==s.name====张三 main == s.age == == 18
//指针传递修改了实参的值
fun02(&s); //main==s.name====李四 main == s.age == == 20
cout << "main==s.name====" << s.name << endl;
cout << "main==s.age====" << s.age << endl;
system("pause");
return EXIT_SUCCESS;
}
struct info fun03()
{
struct info s;
strcpy(s.name, "李四");
s.age = 20;
return s;
}
struct info * fun04()
{
struct info s;
strcpy(s.name, "李四");
s.age = 20;
return &s;
}
int main()
{
/*返回值类型是结构体*/
struct info s = fun03();//main==s.name====李四 main == s.age == == 20
cout << "main==s.name====" << s.name << endl;
cout << "main==s.age====" << s.age << endl;
//值打印不出来,因为fun04中的结构体是在栈区创建s1对应的值已经被销毁
struct info * s1 = fun04();//main==s1->name====烫烫沺c荀 main == s1->age == == 264749488
cout << "main==s1->name====" << s1->name << endl;
cout << "main==s1->age====" << s1->age << endl;
system("pause");
return EXIT_SUCCESS;
}
结构体填充结构体
#include "iostream"
using namespace std;
struct stra
{
int a; //4 4
float b;//4 8
char c;//1 12 9
char arr[7]; //16
double h;//24
}abc;
struct strb
{
struct stra abc;//12 16
short f; //2
char * e; //4
short g;
double d; //8
};
int main()
{
struct strb strbb;
strbb.d = 10.0f;
strbb.abc.a = 100;
cout << "strbb.abc.a====" << strbb.abc.a << endl;
cout << "sizeof(strbb)====" << sizeof(strbb) << endl;
//strbb.abc.a====100
//sizeof(strbb)====48
system("pause");
return EXIT_SUCCESS;
}