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;
}