将指向 char 数组的指针作为参数传递给函数 - C [英] Passing a pointer to a char array as an argument to a function - C

查看:86
本文介绍了将指向 char 数组的指针作为参数传递给函数 - C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的函数声明中,第一个参数是一个字符串,具体来说是一个字符数组,第三个参数是一个整数指针.第二个参数是指向字符数组的指针吗?换句话说,指向指针的指针?我在阅读了相关问题的答案后发现了这一点:C 中将数组和数组指针传递给函数的区别

In the following function declaration, the first argument is a String, specifically, an array of chars, and the third argument is a pointer to an integer. Is the second argument a pointer to an array of chars? In other words, a pointer to a pointer? I find this after reading this answer to a related question: Difference between passing array and array pointer into function in C

void setup(char inputBuffer[], char *args[], int *background) {...}

换句话说,*args[] 等价于 **args 吗?

In other words, is *args[] equivalent to **args?

非常感谢!

推荐答案

是的,在向函数传递参数时,char *args[] 等价于 char **args代码>.

Yes, in passing arguments to a function, char *args[] is equivalent to char **args.

在第一个参数 char inputBuffer[] 中,该函数实际上接收的不是整个 char 数组,而是一个保存其第一个元素地址的指针变量.

In the first argument, char inputBuffer[], the function actually receives not the whole char array but only a pointer variable holding the address of its first element.

在第二个参数 char *args[] 中,类似地,函数接收的不是指向 char 的整个指针数组,而是一个保存地址的指针变量第一个元素.在这种情况下,元素本身就是一个指针.因此,该函数接收一个指向 char 指针的指针,相当于 char **args.

In the second argument, char *args[], similarly, the function receives not the whole array of pointers to chars, but a pointer variable holding the address of the first element. In this case the element is itself a pointer. Therefore the function receives a pointer to a char pointer, equivalent to char **args.

这篇关于将指向 char 数组的指针作为参数传递给函数 - C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆