C语言中使用struct实现数组复制
来源:互联网 发布:数据库的逻辑模型图 编辑:程序博客网 时间:2024/06/01 10:56
在C语言中,直接在参数中声明数组变量是无法实现传值传参的,这时只会把数组的首地址传过去。将数组放置到一个struct中进行传参,可以实现传值的方式。例如下面的程序:
#include <stdio.h>struct tag {int a[100]};struct tag orange, lime, lemon;struct tag twofold(struct tag s){int i;for(i=0; i<100; i++){s.a[i] *= 2;}return s;}int main(){int i;for(i=0; i<100; i++){ orange.a[i] = 1; }lime = twofold(orange);for(i=0; i<10; i++){printf("before: lime.a[%d] = %d\n", i, lime.a[i]);}lemon = lime;for(i=0; i<100; i++){ lemon.a[i] = 7; }for(i=0; i<10; i++){printf("lemon.a[%d] = %d\n", i, lemon.a[i]);}for(i=0; i<10; i++){printf("after: lime.a[%d] = %d\n", i, lime.a[i]);}}
输出结果:
before: lime.a[0] = 2before: lime.a[1] = 2before: lime.a[2] = 2before: lime.a[3] = 2before: lime.a[4] = 2before: lime.a[5] = 2before: lime.a[6] = 2before: lime.a[7] = 2before: lime.a[8] = 2before: lime.a[9] = 2lemon.a[0] = 7lemon.a[1] = 7lemon.a[2] = 7lemon.a[3] = 7lemon.a[4] = 7lemon.a[5] = 7lemon.a[6] = 7lemon.a[7] = 7lemon.a[8] = 7lemon.a[9] = 7after: lime.a[0] = 2after: lime.a[1] = 2after: lime.a[2] = 2after: lime.a[3] = 2after: lime.a[4] = 2after: lime.a[5] = 2after: lime.a[6] = 2after: lime.a[7] = 2after: lime.a[8] = 2after: lime.a[9] = 2
参考《C专家编程》
- C语言中使用struct实现数组复制
- C语言struct复制问题
- C语言变长数组实现(利用 struct中char data[0] )
- C语言中struct数组乱序赋值
- C语言中 typedef struct 和 struct
- C语言中使用指针实现数组排序
- 使用C语言的struct来实现C++的class
- C语言中结构体struct类型使用
- 如何利用C语言实现数组复制的功能
- C语言中用struct封装一个数组
- C语言中动态数组的实现
- c语言中动态数组的实现
- C语言中实现字符串数组
- C语言中实现字符串数组
- C语言中动态数组的实现
- C语言中动态数组的实现
- 使用数组实现队列(C语言)
- 使用数组实现栈(C语言)
- 操作系统 第四章 存储管理
- JavaScript 数据类型
- CPU性能分析工具之OProfile !
- 关于Socket.Receive()与Send()的常见问题
- poj 1552
- C语言中使用struct实现数组复制
- 操作系统 第五章 文件管理
- win2003+iis配置访问.asp,.aspx,.php页面错误解决一栏表
- C#日期控件变为时间控件
- Office彩蛋 关于office中隐藏的小秘密(或小游戏)
- Android学习之pmem driver
- Franklin C-51语言程序设计基础
- 如何在linux/unix中设置线程的优先级
- 每日一练