C语言 传参...
时间: 2023-07-18 13:07:24 浏览: 51
在 C 语言中,函数参数传递分为两种方式:值传递和指针传递。
值传递是指将实参的值复制给形参,函数内部对形参的修改不会影响到实参。例如:
```c
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 1, y = 2;
swap(x, y);
printf("x=%d, y=%d\n", x, y); // 输出 x=1, y=2
return 0;
}
```
指针传递是指将实参的地址传递给形参,函数内部对形参指向的内存空间的修改会影响到实参。例如:
```c
void swap(int *pa, int *pb) {
int temp = *pa;
*pa = *pb;
*pb = temp;
}
int main() {
int x = 1, y = 2;
swap(&x, &y);
printf("x=%d, y=%d\n", x, y); // 输出 x=2, y=1
return 0;
}
```
注意,在指针传递中,需要使用取地址运算符 `&` 将变量的地址传递给形参,而在函数内部使用指针访问变量时,需要使用解引用运算符 `*`。
相关问题
c语言函数传参 数组
在C语言中,函数传参数组可以有两种方式:传递数组的指针或传递数组的首元素地址。具体来说,可以使用以下两种方式:
1. 传递数组的指针
函数定义时,可以使用指针作为参数,这个指针指向数组的首地址。在函数内部,可以通过指针访问数组中的元素。
例如:
```c
void printArray(int *arr, int len) {
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr) / sizeof(int);
printArray(arr, len);
return 0;
}
```
2. 传递数组的首元素地址
函数定义时,可以使用数组作为参数,这个数组会被自动转换为指向首元素的指针。在函数内部,也可以通过指针访问数组中的元素。
例如:
```c
void printArray(int arr[], int len) {
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr) / sizeof(int);
printArray(arr, len);
return 0;
}
```
c语言函数结构体传参
在C语言中,可以通过结构体指针来实现函数结构体传参。通过传递结构体指针,可以避免在函数调用过程中复制整个结构体的开销,提高程序的运行效率。通过指针传递结构体,可以直接修改结构体中的成员值,使得函数调用后可以改变原始结构体的值。
具体实现方式如下所示:
1. 定义一个结构体,包含需要传递的数据。
```c
struct student {
int num;
char name = 100;
strcpy(p->name, "jerry");
}
```
3. 在调用函数时,将结构体的地址传递给函数。
```c
struct student stu;
stu.num = 12345;
strcpy(stu.name, "Tom");
stu.score = 67.5;
stu.score = 89;
stu.score = 78.6;
change(&stu);
```
通过上述步骤,我们可以看到在函数`change`中,通过结构体指针修改了结构体内的数据。这样可以在函数调用后改变原始结构体的值。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [C语言关于结构体做参数传递](https://edu.csdn.net/skill/c/c-11a69657ca7d422d9a08ecef807f1600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [java 与C语言传递结构体数据](https://download.csdn.net/download/haozisex/9849782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)