c语言-函数-009

2.函数传参:
2.1赋值传递(复制传递)函数体内部想要使用函数体外部变量值的时候使用复制传递2.2全局变量传递
#include <stdio.h>int Num1 = 100;
int Num2 = 200;
int Ret = 0;void Add(void)
{Ret = Num1 + Num2;return;
}int main(void)
{Add();printf("Ret = %d\n", Ret);return 0;
}
2.3地址传递函数体内部想要修改函数体外部变量值的时候使用地址传递

示例1:

#include <stdio.h>int SetNum(int *pTmp)
{*pTmp = 100;return 0;
}int main(void)
{int Num = 0;SetNum(&Num);printf("Num = %d\n", Num);return 0;
}

示例2:

#include <stdio.h>int Swap(int *px, int *py)
{int tmp = 0;tmp = *px;*px = *py;*py = tmp;return 0;
}int main(void)
{int Num1 = 100;int Num2 = 200;Swap(&Num1, &Num2);printf("Num1 = %d, Num2 = %d\n", Num1, Num2);return 0;
}
    函数体内想修改函数体外指针变量值的时候传指针变量的地址即二级指针
#include <stdio.h>int SetPoint(char **pptmp)
{*pptmp = "hello world";return 0;
}int main(void)
{char *p = NULL;SetPoint(&p);printf("p = %s\n", p);return 0;
}
2.4整形数组传递int a[5] = {1, 2, 3, 4, 5};int Fun(int parray[5]);int Fun(int parray[], int len);int Fun(int *parray, int len);

示例1:

#include <stdio.h>#if 0
int PrintArray1(int parray[5])
{int i = 0;printf("sizeof:%ld\n", sizeof(parray));for (i = 0; i < 5; i++){printf("%d ", parray[i]);}printf("\n");return 0;
}int PrintArray2(int parray[], int len)
{int i = 0;printf("sizeof:%ld\n", sizeof(parray));for (i = 0; i < len; i++){printf("%d ", parray[i]);}printf("\n");return 0;
}
#endifint PrintArray3(int *parray, int len)
{int i = 0;for (i = 0; i < len; i++){printf("%d ", parray[i]);}printf("\n");return 0;
}int main(void)
{int a[5] = {1, 2, 3, 4, 5};//	PrintArray1(a);
//	PrintArray2(a, 5);PrintArray3(a, 5);return 0;
}

示例2:

#include <stdio.h>int Fun(int (*p)[3], int len)
{int i = 0;int j = 0;for (j = 0; j < len; j++){for (i = 0; i < 3; i++){printf("%d ", p[j][i]);}printf("\n");}return 0;
}int main(void)
{int a[2][3] = {1, 2, 3, 4, 5, 6};Fun(a, 2);return 0;
}
2.5字符型数组和字符串的传递char str[32] = {"hello world"};int Fun(char *pstr);2.6二维数组传递
(1)整形二维数组传递int a[2][3] = {1, 2, 3, 4, 5, 6};int Fun(int (*parray)[3], int len);
(2)字符型二维数组传递char str[5][32] = {"hello", "world", "how", "are", "you"}; int Fun(char (*pstr)[32], int len);
#include <stdio.h>int Fun(char (*pstr)[32], int len)
{int i = 0;for (i = 0; i < len; i++){printf("pstr[%d] = %s\n", i, pstr[i]);}return 0;
}int FunPointArray(char **parray, int len)
{int i = 0;for (i = 0; i < len; i++){printf("parray[%d] = %s\n", i, parray[i]);}return 0;
}int main(void)
{char str[5][32] = {"hello", "world", "how", "are", "you"};char *a[5] = {str[0], str[1], str[2], str[3], str[4]};int i = 0;Fun(str, 5);FunPointArray(a, 5);return 0;
}
2.7指针数组传递char *pstr[5] = {NULL};int Fun(char **ppstr, int len);

2.8结构体传递
(1)结构体变量传递
struct student s;

int Fun(struct student tmp);

#include <stdio.h>struct student
{char name[32];char sex;char age;int score;
};struct student GetStuInfo(void)
{struct student stu;gets(stu.name);scanf("%c", &stu.sex);scanf("%d", &stu.age);scanf("%d", &stu.score);return stu;
}void PutStuInfo(struct student tmp)
{printf("姓名:%s\n", tmp.name);printf("性别:%c\n", tmp.sex);printf("年龄:%d\n", tmp.age);printf("成绩:%d\n", tmp.score);return;
}int main(void)
{struct student s;s=GetStuInfoByPoint(&s);PutStuInfo(s);return 0;
}

(2)结构体指针传递
struct student s;

int Fun(struct student *ps);

#include <stdio.h>struct student
{char name[32];char sex;char age;int score;
};int GetStuInfoByPoint(struct student *ps)
{gets(ps->name);scanf("%c", &ps->sex);scanf("%d", &ps->age);scanf("%d", &ps->score);return 0;
}int PutStuInfoByPoint(struct student *ps)
{printf("姓名:%s\n", ps->name);printf("性别:%c\n", ps->sex);printf("年龄:%d\n", ps->age);printf("成绩:%d\n", ps->score);return 0;
}int main(void)
{struct student s;GetStuInfoByPoint(&s);PutStuInfoByPoint(&s);return 0;
}

(3)结构体数组传递
struct student stu[3];

int Fun(struct student *pstu, int len);

#include <stdio.h>struct student 
{char name[32];char sex;int age;int score;
};int PrintStuInfo(struct student *pstu, int len)
{int i = 0;for (i = 0; i < len; i++){printf("姓名:%s\n", pstu[i].name);printf("性别:%c\n", pstu[i].sex);printf("年龄:%d\n", pstu[i].age);printf("成绩:%d\n", pstu[i].score);}return 0;
}int main(void)
{struct student stu[3] = {{"zhangsan", 'm', 19, 100},{"lisi", 'f', 18, 90},{"wanger", 'm', 17, 60},};PrintStuInfo(stu, 3);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/722001.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深度解析速卖通商品详情API:Python实战与高级技术探讨

速卖通商品详情API接口实战&#xff1a;Python代码示例 一、准备工作 在开始之前&#xff0c;请确保你已经完成了以下步骤&#xff1a; 在速卖通开放平台注册账号并创建应用&#xff0c;获取API密钥。阅读速卖通商品详情API接口的文档&#xff0c;了解接口的使用方法和参数要…

什么是物联网?物联网如何工作?

物联网到底是什么&#xff1f; 物联网(Internet of Things&#xff0c;IoT)的概念最早于1999年被提出&#xff0c;官方解释为“万物相连的互联网”&#xff0c;是在互联网基础上延伸和扩展&#xff0c;将各种信息传感设备与网络结合起来而形成的一个巨大网络&#xff0c;可以实…

[SpringCloud] OpenFeign核心架构原理 (一)

Feign的本质: 动态代理 七大核心组件 Feign底层是基于JDK动态代理来的, Feign.builder()最终构造的是一个代理对象, Feign在构建对象的时候会解析方法上的注解和参数, 获取Http请求需要用到基本参数以及和这些参数和方法参数的对应关系。然后发送Http请求, 获取响应, 再根据响…

Python Web开发记录 Day6:MySQL(关系型数据库)

名人说&#xff1a;莫道桑榆晚&#xff0c;为霞尚满天。——刘禹锡&#xff08;刘梦得&#xff0c;诗豪&#xff09; 创作者&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 目录 六、MySQL1、MySQL-概述和引入①MySQL是什么&am…

liunx安装jdk、redis、nginx

jdk安装 下载jdk,解压。 sudo tar -zxvf /usr/local/jdk-8u321-linux-x64.tar.gz -C /usr/local/ 在/etc/profile文件中的&#xff0c;我们只需要编辑一下&#xff0c;在文件的最后加上java变量的有关配置&#xff08;其他内容不要动&#xff09;。 export JAVA_HOME/usr/l…

docker部署aria2-pro

前言 我平时有一些下载视频和一些资源文件的需求&#xff0c;有时候需要离线下载&#xff0c;也要速度比较快的方式 之前我是用家里的玩客云绝育之后不再写盘当下载机用的&#xff0c;但是限制很多 我发现了aria2 这个下载器非常适合我&#xff0c;而有个大佬又在原来的基础…

10 OpenCV 形态学的应用

文章目录 算子形态学提取直线示例 算子 adaptiveThreshold 二值化算子 adaptiveThreshold(src, dstNone,maxValue, adaptiveMethod, thresholdType, blockSize, C, ) /* *src&#xff1a;灰度化的图片 *dst&#xff1a;输出图像&#xff0c;可选 *maxValue&#xff1a;满足条件…

关于程序员如何选择职业赛道

程序员作为一个独具特色的职业群体&#xff0c;面临着诸多挑战和机遇。在选择职业赛道方面&#xff0c;程序员需要考虑自身兴趣、技能、发展前景等因素&#xff0c;以便找到最适合自己的发展路径。本文将从不同角度探讨程序员如何选择职业赛道。 首先&#xff0c;程序员应该认…

C#中对象的相等性与同一性的判断方法总结

C#对象的相等性与同一性 1. 概述与准备1.1 概述1.2 准备 2. Equals(Object)2.1 功能&#xff1a;2.2 实例&#xff1a;2.3 扩展&#xff1a;2.4 重写此方法 3. Equals(Object, Object)3.1 功能3.2 实例 4. ReferenceEquals(Object, Object)4.1 功能4.2 使用场景&#xff1a;4.3…

人工智能+

上上一个风口是互联网&#xff0c;信息分享。 上一个风口是物联网&#xff0c;实现万物互联。 如今再提人工智能&#xff0c;传感器大数据AI算法&#xff0c;尽量地减少人为干预&#xff0c;替代人工作或实现人无法执行的工作。 弱人工智能 所谓弱人工智能就是仅在单个领域…

Java必须掌握的多态的优势和弊端(含面试大厂题和源码)

Java中的多态是面向对象编程的核心特性之一&#xff0c;它允许一个引用类型变量在运行时绑定到多个不同的类型的对象。多态的使用带来了许多优势&#xff0c;同时也存在一些潜在的弊端。在面试大厂时&#xff0c;理解和能够讨论这些优缺点显示出深入的知识和对技术的全面理解。…

桥梁工程AR增强现实模拟情景实训教学演练

在传统的桥梁工程专业课堂中&#xff0c;理论知识的学习往往占据了大部分时间。然而&#xff0c;对于桥梁工程这样的专业领域&#xff0c;实践操作的重要性不言而喻。而AR技术的出现&#xff0c;恰恰解决了这个问题。 首先&#xff0c;AR技术可以模拟真实的桥梁环境&#xff0c…

数据结构学习(四)高级数据结构

高级数据结构 1. 概念 之所以称它们为高级的数据结构&#xff0c;是因为它们的实现要比那些常用的数据结构要复杂很多&#xff0c;能够让我们在处理复杂问题的过程中&#xff0c; 多拥有一把利器&#xff0c;同时掌握好它们的性质&#xff0c;以及所适应的场合&#xff0c;在…

《剑指offer》76--删除链表中重复的结点[C++]

目录 题目&#xff1a; 思路&#xff1a; 贴代码&#xff1a; 代码输出 题目&#xff1a; 在一个排序的链表中&#xff0c;存在重复的结点&#xff0c;请删除该链表中重复的结点&#xff0c;重复的结点不保留&#xff0c;最后返回链表头指针。 如&#xff1a; 链表1->…

Windows下定时器SetTimer以及KillTimer的用法

前言 在Windows下&#xff0c;定时器通常用于周期性地执行某些任务或在一定延迟后执行特定的操作。Windows提供了一些API函数来操作定时器&#xff0c;其中主要包括 SetTimer、KillTimer 和 SetTimerProc。 什么时候我们需要用到SetTimer函数呢&#xff1f;当你需要每个一段时…

PaddleOCR基于PPOCRv4的垂类场景模型微调——手写文字识别

PaddleOCR手写文字识别 一. 项目背景二. 环境配置三. 数据构造四. 模型微调五. 串联推理六. 注意事项七. 参考文献 光学字符识别&#xff08;Optical Character Recognition, OCR&#xff09;&#xff0c;ORC是指对包含文本资料的图像文件进行分析识别处理&#xff0c;获取文字…

EXTJS实现自定义表格

宽度自适应 width: 100%, 高度自适应 height: 100% 同时设置表格所处页面高度100% html,body,#griddemo{height: 100%;} 自定义显示的文本内容 Ext.onReady(function () {Ext.QuickTips.init()function sexText(val) {if (val 0) {return <span style"color:green…

【牛客】SQL135 每个6/7级用户活跃情况

描述 现有用户信息表user_info&#xff08;uid用户ID&#xff0c;nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间&#xff09;&#xff1a; iduidnick_nameachievementleveljobregister_time11001牛客1号31007算法2020-01-01 10:00:00210…

网络编程的学习

思维导图 多路复用代码练习 select完成TCP并发服务器 #include<myhead.h> #define SER_IP "192.168.125.73" //服务器IP #define SER_PORT 8888 //服务器端口号int main(int argc, const char *argv[]) {//1、创建用于监听的套接字int sfd -1;s…

numpy基础运算

numpy基础运算 import numpy as npt1 np.array([1, 2, 3, 4, 5]) # numpy数组类型为numpy.ndarray print("type(np.array)", type(t1)) t2 np.array(range(6)) print("t1:", t1) print("t2:", t2)# np.arange([start,] stop [, stop, ], dtyp…