当前位置: 首页 > news >正文

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.lryc.cn/news/311535.html

相关文章:

  • Spring事件发布监听器ApplicationListener原理- 观察者模式
  • 系统学习Python——装饰器:直接管理函数和类
  • Leetcode 3049. Earliest Second to Mark Indices II
  • CrossOver 24下载-CrossOver 24 for Mac下载 v24.0.0中文永久版
  • 算法设计.
  • 20240304金融读报:票据贴现数据挖掘与新质生产力信贷创新
  • 05. Nginx入门-Nginx访问控制
  • S2---FPGA-A7板级原理图硬件实战
  • RK DVP NVP6158配置 学习
  • C++基础2:C++基本数据类型和控制结构
  • HFSS仿真双频微带天线学习笔记
  • 【十一】【SQL】外连接(左外连接,右外连接)
  • 敏捷开发模型:一种灵活、协作和持续的软件开发方法
  • 软件设计师10--计算机组成与体系结构章节回顾
  • 数据库分库分表中间件选择
  • 代码随想录算法训练营第22天|235.二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点
  • 基于SpringBoot的医护人员排班系统详细开题报告(源码)
  • CDH6.3.1离线安装
  • Pytorch之卷积操作
  • 2024年春招小红书前端实习面试题分享
  • 软件测试--性能测试工具JMeter
  • c++/c图的邻近矩阵表示
  • cocos-lua定时器用法
  • 激活函数Swish(ICLR 2018)
  • 【C++ 标准流,文件流】
  • 【排序】详解冒泡排序
  • 什么是Docker容器?
  • (C++练习)选择题+编程题
  • 【鸿蒙开发】第十五章 ArkTS基础类库-并发
  • 华为数通方向HCIP-DataCom H12-821题库(多选题:21-40)