C语言 结构体作为函数的参数
1)使用结构体变量作为函数的参数
使用结构体变量作为函数的实参时,采用的是值传递,会将结构体变量所占内存单元的内容全部顺序传递给形参,形参必须是同类型的结构体变量
demo:
# include <stdio.h>
# include <stdlib.h> //创建一个Student结构
struct Student
{
char name[];
float fScore[];
}student={"dire",98.5,89.0,93.5}; //初始化结构体变量 void Display(struct Student su) //形参为同类型的结构体(Student结构)
{
printf("-----Information------\n");
printf("Name:%s",su.name);
printf("Chinese:%.2f\n",su.fScore[]);
printf("Math:%.2f\n",su.fScore[]);
printf("English:%.2f",su.fScore[]);
printf("平均分数为:%.2f\n",(su.fScore[]+su.fScore[],su.fScore[])/);
} int main ()
{ Display(student); return ;
}
Printf:
2)使用指向结构体变量的指针作为函数参数
Demo:
# include <stdio.h>
# include <stdlib.h> struct Student {
char name[];
float fScore[];
}student = {"dire",98.5,89.0,93.5}; //初始化结构体变量 void Display(struct Student *pStruct)
{
printf("------Information-------\n");
printf("Name:%s\n",pStruct->name);
printf("Chinese:%.2f\n",(*pStruct).fScore[]);
printf("Math:%.2f\n",(*pStruct).fScore[]);
printf("English:%.2f\n",pStruct->fScore[]);
} int main ()
{
Display(&student); //将结构体变量的首地址作为实参传入pStruct指针变量中 return ;
}
3)使用结构体变量的成员作为函数参数
这种方式为函数传递参数与普通的变量作为实参是一样的,是值传递
C语言 结构体作为函数的参数的更多相关文章
- go语言结构体作为函数参数,采用的是值传递
经过验证,go语言结构体作为函数参数,采用的是值传递.所以对于大型结构体传参,考虑到值传递的性能损耗,最好能采用指针传递. 验证代码: package main import ( "fmt& ...
- 深入理解C语言-结构体做函数参数
结构体做函数参数,在C语言中属于常见现象,此时为了内存考虑,不传递结构体,而是传递结构体的地址 结构体定义 struct Man { char name[64]; int age; }; 结构体可以与 ...
- C语言结构体和函数
#include <stdio.h> struct Person { char *name; }; void change1(struct Person p); void change2( ...
- C语言结构体及函数传递数组參数演示样例
注:makeSphere()函数返回Sphere结构体,main函数中.调用makeSphere()函数,传递的第一个參数为数组,传递的数组作为指针.
- golang中结构体当做函数参数或函数返回值都会被拷贝
1. 结构体做函数的参数或返回值时,都会被重新拷贝一份如果不想拷贝,可以传递结构体指针 package main import "fmt" type Person struct { ...
- go语言基础之结构体做函数参数 值传递和地址传递
1.结构体做函数参数值传递 示例: package main //必须有个main包 import "fmt" //定义一个结构体类型 type Student struct { ...
- 在C语言结构体中添加成员函数
我们在使用C语言的结构体时,经常都是只定义几个成员变量,而学过面向对象的人应该知道,我们定义类时,不只是定义了成员变量,还定义了成员方法,而类的结构和结构体非常的相似,所以,为什么不想想如何在C语言结 ...
- 【C++】结构体/结构体数组/结构体指针/结构体嵌套/函数参数/const
一.结构体声明 struct Student { //成员列表 string name; int age; int score; }; //s3;定义时直接声明 int main() { struct ...
- c语言结构体
[C语言]21-结构体 本文目录 一.什么是结构体 二.结构体的定义 三.结构体变量的定义 四.结构体的注意点 五.结构体的初始化 六.结构体的使用 七.结构体数组 八.结构体作为函数参数 九.指向结 ...
随机推荐
- Docker Daemon 连接方式详解
前言 在 Docker 常用详解指令 一文中粗粗提了一下, Docker 是分为客户端和服务端两部分的, 本文将介绍客户端是如何连接服务端的. 连接方式 1. UNIX域套接字 默认就是这种方式, 会 ...
- linux下的用户管理(一)
linux下有三类用户: 1.超级用户:root具有操作系统的一切权限,UID值为0的是超级用户 2.普通用户:只具有操作系统有限的权限,UID是从500到6000范围 3.伪用户:是为了方便系统管理 ...
- 【自己的练习git】自己的git练习
liqiang@username MINGW64 ~/Desktop$ mkdir TestGit 新建目录 liqiang@username MINGW64 ~/Desktop$ cd ...
- Document类
一.类结构 org.jsoup.nodes Class Document java.lang.Object org.jsoup.nodes.Node org.jsoup.nodes.Element o ...
- java中Map的entrySet 和keySet的使用
存储这样的一个数据关系结构 使用嵌套map存储 可以通过调用 entrySet方法 或者 keySet方法 进行迭代或者增强for循环 便利输出 这里演示 迭代器的方式进行遍历 package ...
- hdu 4991(树状数组+DP)
Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Codeforces 898 A. Rounding
A. Rounding time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- ansible-playbook启动的多种方式
#quick start ## start the playbook with no password, it will run "sudo su - root" at the t ...
- SpringBoot整合MyBatisPlus配置动态数据源
目录 SpringBoot整合MyBatisPlus配置动态数据源 SpringBoot整合MyBatisPlus配置动态数据源 推文:2018开源中国最受欢迎的中国软件MyBatis-Plus My ...
- 聊聊、Zookeeper Windows启动
Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly rel ...