实现结构体变量交换值的函数
2004-12-27 22:01
232 查看
#include <stdio.h>
#include <string.h>
struct stuff
{
char full_name[20];
unsigned age;
unsigned gender;
};
void swap_struct_data(char *ptr_a, char *ptr_b, int size)
{
/*函数:swap_struct_data
作用:交换两个结构型变量的值
参数:ptr_a 变量A的地址,ptr_b 变量B的地址,size 变量的大小*/
char p;
while (size--!=0)
{
p=*ptr_a;
*ptr_a=*ptr_b;
*ptr_b=p;
ptr_a++;
ptr_b++;
}
}
int main()
{
/* 定义两个stuff型的变量,并初始化 */
struct stuff stu_a;
struct stuff stu_b;
strcpy(stu_a.full_name,"hexuhui");
stu_a.age=60;
stu_a.gender=1;
strcpy(stu_b.full_name,"candy");
stu_b.age=20;
stu_b.gender=2;
/* 显示交换数据前的值*/
printf("Data in stu_a:/n");
printf("%s/n",stu_a.full_name);
printf("%d/n",stu_a.age);
printf("%d/n",stu_a.gender);
printf("Data in stu_b:/n");
printf("%s/n",stu_b.full_name);
printf("%d/n",stu_b.age);
printf("%d/n",stu_b.gender);
/* 调用swap_struct_data函数,取出stu_a和stu_b的地址,转换成字符型指针后传入函数,
sizeof(struct stuff)是结构体的大小 */
swap_struct_data((char *)&stu_a,(char *)&stu_b,sizeof(struct stuff));
/* 显示交换数据后的值*/
printf("Data in stu_a after call function:/n");
printf("%s/n",stu_a.full_name);
printf("%d/n",stu_a.age);
printf("%d/n",stu_a.gender);
printf("Data in stu_b after call function:/n");
printf("%s/n",stu_b.full_name);
printf("%d/n",stu_b.age);
printf("%d/n",stu_b.gender);
}
#include <string.h>
struct stuff
{
char full_name[20];
unsigned age;
unsigned gender;
};
void swap_struct_data(char *ptr_a, char *ptr_b, int size)
{
/*函数:swap_struct_data
作用:交换两个结构型变量的值
参数:ptr_a 变量A的地址,ptr_b 变量B的地址,size 变量的大小*/
char p;
while (size--!=0)
{
p=*ptr_a;
*ptr_a=*ptr_b;
*ptr_b=p;
ptr_a++;
ptr_b++;
}
}
int main()
{
/* 定义两个stuff型的变量,并初始化 */
struct stuff stu_a;
struct stuff stu_b;
strcpy(stu_a.full_name,"hexuhui");
stu_a.age=60;
stu_a.gender=1;
strcpy(stu_b.full_name,"candy");
stu_b.age=20;
stu_b.gender=2;
/* 显示交换数据前的值*/
printf("Data in stu_a:/n");
printf("%s/n",stu_a.full_name);
printf("%d/n",stu_a.age);
printf("%d/n",stu_a.gender);
printf("Data in stu_b:/n");
printf("%s/n",stu_b.full_name);
printf("%d/n",stu_b.age);
printf("%d/n",stu_b.gender);
/* 调用swap_struct_data函数,取出stu_a和stu_b的地址,转换成字符型指针后传入函数,
sizeof(struct stuff)是结构体的大小 */
swap_struct_data((char *)&stu_a,(char *)&stu_b,sizeof(struct stuff));
/* 显示交换数据后的值*/
printf("Data in stu_a after call function:/n");
printf("%s/n",stu_a.full_name);
printf("%d/n",stu_a.age);
printf("%d/n",stu_a.gender);
printf("Data in stu_b after call function:/n");
printf("%s/n",stu_b.full_name);
printf("%d/n",stu_b.age);
printf("%d/n",stu_b.gender);
}
相关文章推荐
- 结构体变量作函数参数实现传值调用。
- 计算该日在本年中是第几天?注意瑞年问题。(分别用结构体变量和函数实现)
- assign() 功能的实现 数组分配到模板。使用 extract() 函数。 变量分配到模板。使用 compact()
- php unset()函数销毁变量但没有实现内存释放
- 定义一个结构体变量(包含年月日),写一个函数,计算该日期在本年中为第几天?要求写一个days函数。参数是此结构体类型的变量,返回值是整数。
- c 结构体 & 函数指针模拟实现一个java class(类) 和方法
- 第二周项目3-文件+结构体实现实用函数
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 关于Visual Assistant X助手在定位函数及结构体成员变量不准确的问题
- Linux实现函数和全局变量的#ifdef功能
- 不用任何变量实现strlen()函数
- 函数的同名变量实现可变函数
- 用位运算实现两个变量交换值(不需额外空间)
- C语言学习笔记【结构体02】结构体指针变量与结构体变量的函数参数
- python通过函数属性实现全局变量的方法
- vim安装SrcExpl 插件,实现自动显示跳转函数及变量定义功能
- 结构体变量作为函数的参数和返回值 .
- 郝斌的C语言基础 159-161 通过函数完成对结构体变量的输入输出
- 函数和递归(组合数,孪生素数,用指针实现变量交换,求解二院一次方程组)
- C++小课堂--第一期---面向对象之前 --- 实现函数返回结构体,实现大数据的传输