• c语言-函数-009


    2.函数传参:
    2.1赋值传递(复制传递)
        函数体内部想要使用函数体外部变量值的时候使用复制传递
        
    
    2.2全局变量传递
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #include 
    
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    2.3地址传递
        函数体内部想要修改函数体外部变量值的时候使用地址传递
    
    • 1
    • 2

    示例1:

    #include 
    
    int SetNum(int *pTmp)
    {
    	*pTmp = 100;
    
    	return 0;
    }
    
    int main(void)
    {
    	int Num = 0;
    
    	SetNum(&Num);
    	
    	printf("Num = %d\n", Num);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    示例2:

    #include 
    
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
        函数体内想修改函数体外指针变量值的时候传指针变量的地址即二级指针
    
    • 1
    #include 
    
    int SetPoint(char **pptmp)
    {
    	*pptmp = "hello world";
    
    	return 0;
    }
    
    int main(void)
    {
    	char *p = NULL;
    
    	SetPoint(&p);
    
    	printf("p = %s\n", p);
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    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
    • 2
    • 3
    • 4
    • 5
    • 6

    示例1:

    #include 
    
    #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;
    }
    #endif
    
    int 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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    示例2:

    #include 
    
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    #include 
    
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    2.7指针数组传递
        char *pstr[5] = {NULL};
    
        int Fun(char **ppstr, int len);
    
    • 1
    • 2
    • 3
    • 4

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

    int Fun(struct student tmp);

    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

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

    int Fun(struct student *ps);

    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

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

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

    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    AI电话机器人能否代替人工?优缺点介绍
    [NOIP2009 普及组] 分数线划定
    基于Openwrt系统架构,实现应用与驱动的实例。
    Lumerical---FDE(Finite Difference Eigenmode) Solver
    【SA8295P 源码分析 (一)】76 - Thermal 功耗 之 /dev/thermalmgr 相关调试命令汇总
    Go Atomic
    用于安装和维护光纤单模和多模的光纤网络测试套件
    代码随想录动态规划——不同路径
    基于springboot的社区问答系统的设计与实现
    fast planner代码解析--planner_manager.cpp
  • 原文地址:https://blog.csdn.net/qq_63713157/article/details/136443289