朋友圈网页怎么制作,优化网站有哪些方法,珠海做网站推广公司,重庆建设工程信息网官1.结构体定义时的注意事项#xff1a; 1.错误定义结构体#xff1a;
struct students
{char name[9] Mike;int height 185;
}; 这是不对的#xff0c;在 C 语言中#xff0c;这是由语言的设计原则所决定的。结构体的定义#xff08;struct declaration 1.错误定义结构体
struct students
{char name[9] Mike;int height 185;
}; 这是不对的在 C 语言中这是由语言的设计原则所决定的。结构体的定义struct declaration只是决定了数据的布局和类型。在这个期间并没有为这些数据分配内存。只有当你声明一个特定的结构体变量时内存才会被分配并且你可以在此时为它赋值初始化。这是因为结构体定义是用于创建一种新的类型而不是创建一个变量。类型是一种抽象的概念没有实际的存储空间所以不能为其直接赋值。
2.正确定义结构体 在 C 语言中你不能在结构体定义的时候直接赋初值。应该在声明结构体变量的时候或是在定义之后对单个变量进行初始化操作。
比如
struct students
{char name[9];int height;
};// 在声明变量时进行初始化
struct students aStudent {John, 180};// 或在定义后对单个变量进行初始化
aStudent.height 180;
strcpy(aStudent.name, John); 2.结构体变量赋值
在C语言中主要有三种方式可以为结构体变量赋值
//1. 声明时初始化
struct student {char name[10];int age;
} stu {Tom, 20}; 在这个例子中我们在声明student结构体的时候同时创建了一个名为stu的变量并为其赋值了初始值。
//2. 使用**句点运算符**.
struct student stu;
strcpy(stu.name, Tom);
stu.age 20;在这个例子中我们首先声明了一个名为stu的student结构体变量然后使用句点运算符为其各成员赋值。这里就要注意字符数组或字符串赋值最好使用string.h包含的strcpy()函数赋值方便很多。
//3. **复制另一个相同类型的结构体变量**
struct student stu1 {Tom, 20};
struct student stu2 stu1; 这个方法在交换两个结构变量时非常好用以下举个快速排序学生成绩由高到低的示例展示如何使用以上所说注意事项。
// 引用所需的头文件
#includestdio.h
#includestdlib.h// 定义结构体类型并为成员分配内存空间
typedef struct students
{char name[9]; // 学生姓名允许的最大长度为8个英文字母int height; // 学生身高区间在30-300之间
} Students;// 快速排序算法函数实现
void quick_sort(Students student[], int left, int right)
{int i left;int j right;Students temp1;Students temp2 student[left];if (i j){return; // 递归终止条件i大于j时返回上一级}while (i ! j){// 从右往左找比temp2.height值小的学生while (student[j].height temp2.height i j){j--;}// 从左往右找比temp2.height值大的学生while (student[i].height temp2.height i j){i;}// 交换两个学生位置if (i j){temp1 student[i];student[i] student[j];student[j] temp1;}}// 将基准值放在正确的位置student[left] student[i];student[i] temp2;// 递归调用快速排序算法处理基准值左侧的学生quick_sort(student, left, i - 1);// 递归调用快速排序算法处理基准值右侧的学生quick_sort(student, i 1, right);
}int main()
{// 输入数据int N 0; // 总人数Students student[10001];// 输入学生总数Nscanf(%d, N);// 逐行输入学生的名字和身高for (int i 0; i N; i){scanf(%s %d, student[i].name, student[i].height);}// 使用快速排序算法将学生按身高升序排序quick_sort(student, 0, N - 1);// 输出排序后的学生名单for (int i 0; i N; i){printf(%s %d\n, student[i].name, student[i].height);}return 0;
} 注意看代码中在交换两个结构变量时可以直接把一个变量值赋值给另一个相同类型的结构变量非常方便这里要记住这个用法
运行结果
//测试用例
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159//输出结果
Joe 190
Tom 188
Nick 186
Bob 175
Mike 170
Eva 168
Ann 168
Tim 160
Amy 160
文章转载自: http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.lxbml.cn.gov.cn.lxbml.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.rklgm.cn.gov.cn.rklgm.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.wbysj.cn.gov.cn.wbysj.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.dzzjq.cn.gov.cn.dzzjq.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.gkgb.cn.gov.cn.gkgb.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.gkgb.cn.gov.cn.gkgb.cn http://www.morning.mprky.cn.gov.cn.mprky.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn