C语言与C++语言三种参数传递方式-理解
C语言与C++语言的区别:C0使用gcc成功编译+文件格式为c;C++使用g++编译+文件格式为cpp。
C++语言一般来说函数参数传递方式可以分为三种:1)方式1:值传递;2)方式2:地址传递;3)方式3:引用传递。但是C语言不支持引用传递。
不论基本数据类型int char,还是结构体等复合数据类型,都遵循上述三种传递方式。
#include <stdio.h>
#include <string.h>
void swap_int1(int a, int b);
void swap_int2(int* a, int* b);
void swap_int3(int &a, int &b);
void swap_chararray1(char a[], char b[]);
void swap_charpointer1(char a[], char b[]);
int main(int argc, char* argv[])
{
int x = 9;
int y = 10;
int* px = &x;
int* py = &y;
printf("x=%d y=%d\n", x,y);
swap_int1(x,y);
printf("swap_int1 x=%d y=%d\n", x,y);
swap_int2(px,py);
printf("swap_int2 *px=%d *py=%d\n", *px,*py);
x = 11;
y = 12;
printf(" x=%d y=%d\n", x,y);
swap_int3(x,y);
printf("swap_int3 x=%d y=%d\n", x,y);
char a[128] = "a1234a";
char b[128] = "b4321b";
printf(" a=%s b=%s\n", a,b);
swap_chararray1(a,b);
printf("swap_chararray1 a=%s b=%s\n", a,b);
swap_charpointer1(a,b);
printf("swap_charpointer1 a=%s b=%s\n", a,b);
return 0;
}
void swap_int1(int a, int b)
{
int c = 0;
c = a;
a = b;
b = c;
printf("a=%d b=%d\n", a,b);
}
void swap_int2(int* a, int* b)
{
int c = 0;
c = *a;
*a = *b;
*b = c;
printf("*a=%d *b=%d\n", *a,*b);
}
void swap_int3(int &a, int &b)
{
int c = 0;
c = a;
a = b;
b = c;
printf("a=%d b=%d\n", a,b);
}
void swap_chararray1(char a1[], char b1[])
{
char c[128] = "";
strcpy(c,a1);
strcpy(a1,b1);
strcpy(b1,c);
printf("swap_array1 a1=%s b1=%s\n",a1,b1);
}
void swap_charpointer1(char *a1, char *b1)
{
char c[128] = "";
strcpy(c,a1);
strcpy(a1,b1);
strcpy(b1,c);
printf("swap_charpointer1 a1=%s b1=%s\n",a1,b1);
}
运行结果:
x=9 y=10 a=10 b=9 swap_int1 x=9 y=10 *a=10 *b=9 swap_int2 *px=10 *py=9 x=11 y=12 swap_int3 x=11 y=12 a=a1234a b=b4321b swap_array1 a1=b4321b b1=a1234a swap_chararray1 a=b4321b b=a1234a swap_charpointer1 a1=a1234a b1=b4321b swap_charpointer1 a=a1234a b=b4321b
结构体做函数参数:
#include<stdio.h>
#include<string.h>
struct stud
{
long int num;
float score;
char str[16];
};
void funvr(struct stud t)
{
t.num=2000101;
t.score=71.0;
strcpy(t.str, "funvr");
}
void funar(struct stud t[]) //结构体数组 ==> pointer
{
t[0].num=3000101;
t[0].score=81.0;
strcpy(t[1].str, "funar0");
t[1].num=3000102;
t[1].score=82.0;
strcpy(t[1].str, "funar1");
}
void funpr(struct stud *t)
{
t->num=4000101; /*注意通过结构体指针变量引用成员的具体形式*/
(*t).score=92.0;
strcpy(t->str, "funpr");
}
void funquote(struct stud &t)
{
t.num=5000101;
t.score=101.0;
strcpy(t.str, "funquote");
}
int main(int agrc, char* argv[])
{
struct stud a[2]={
{1000101,61.0,"a0"}, {1000102,62.0,"a1"}
};
struct stud b=a[0],*p;
printf("old b: b.num:%ld\tb.score:%f,b.str=%s\n",b.num,b.score,b.str);
/*显示结构体变量b的成员的原有值*/
funvr(b);
/*验证第一种情况,观察并分析结果,看结构体变量作为函数参数时,形参结构体变量成员的值的改变能影响实参结构体变量的成员的值,
以下为输出调用函数funvr(b)之后的结果值*/
printf("call funvr() new b: b.num:%ld\tb.score:%f,b.str=%s\n ",b.num,b.score,b.str);
funpr(&b); /*将结构体变量的指针对作为函数的参数*/
printf("call funpr() new b: b.num:%ld\tb.score:%f,b.str=%s\n ",b.num,b.score,b.str);
funquote(b); /*将结构体变量的引用对作为函数的参数*/
printf("call funquote() new b: b.num:%ld\tb.score:%f,b.str=%s\n ",b.num,b.score,b.str);
/*输出结构体数组a元素的原来的成员值*/
printf("old a[0]:a[0].num:%ld\ta[0].score:%f,a[0].str=%s\n ",a[0].num,a[0].score,a[0].str);
printf("old a[1]:a[1].num:%ld\ta[1].score:%f,a[1].str=%s\n ",a[1].num,a[1].score,a[1].str);
/*将结构体数组a作为函数的参数,然后再输出其元素的成员的值,已经被修改了*/
funar(a);
printf(" new a[0]:a[0].num:%ld\ta[0].score:%f,a[0].str=%s\n ",a[0].num,a[0].score,a[0].str);
printf(" new a[1]:a[1].num:%ld\ta[1].score:%f,a[1].str=%s\n ",a[1].num,a[1].score,a[1].str);
return 0;
}
运行结果:
old b: b.num:1000101 b.score:61.000000,b.str=a0 call funvr() new b: b.num:1000101 b.score:61.000000,b.str=a0 call funpr() new b: b.num:4000101 b.score:92.000000,b.str=funpr call funquote() new b: b.num:5000101 b.score:101.000000,b.str=funquote old a[0]:a[0].num:1000101 a[0].score:61.000000,a[0].str=a0 old a[1]:a[1].num:1000102 a[1].score:62.000000,a[1].str=a1 new a[0]:a[0].num:3000101 a[0].score:81.000000,a[0].str=a0 new a[1]:a[1].num:3000102 a[1].score:82.000000,a[1].str=funar1
根据自己的实际项目经验
1.先定义结构体变量,然后定义一个指向这个结构体变量的指针,这个指针就可以做函数参数,然后返回在参数中做过修改的数值;
2.也可以定义结构体指针,然后通过malloc方法申请地址,这个指针也可以做函数参数,然后返回在参数中做过修改的数值;
主要参考:
https://blog.csdn.net/tham_/article/details/45370607
https://www.cnblogs.com/wyuzl/p/6248952.html
https://blog.csdn.net/u010414589/article/details/49800305
浙公网安备 33010602011771号