简体   繁体   English

将字符串数组作为参数传递给双指针参数

[英]Passing an array of strings as an argument into a double pointer parameter

For the following code:对于以下代码:

char* fcn(char **para){

}

int main(void){
  char *arg[] = {"XX", "YY", "ZZ"};
  char *s = fcn(arg); 
}

Why is it that when we pass the argument into the function in char *s = fcn(arg);为什么当我们将参数传递给char *s = fcn(arg);中的 function 时, we don't need to do &arg ? ,我们不需要做&arg吗? arg[] seems to me a single pointer, so shouldn't we do &ptr for it to be referenced by a double pointer as in the parameter of fcn(): char* fcn(char **para) ? arg[]在我看来是一个单指针,所以我们不应该像 fcn(): char* fcn(char **para)的参数那样做&ptr让它被双指针引用吗?

An array when used in most expressions decays into a pointer to the first element of the array.在大多数表达式中使用时,数组会衰减为指向数组第一个元素的指针。

arg is declared as an array of char * , so a pointer to an element of the array has type char ** . arg被声明为char *数组,因此指向数组元素的指针具有char **类型。

If you were to use &arg , that is one of the few times an array does not decay and the expression would have type char *(*)[3] , ie a pointer to an array of 3 elements of type char * .如果您要使用&arg ,那是数组衰减且表达式的类型为char *(*)[3]的少数几次之一,即指向char *类型的 3 个元素的数组的指针。 This is very different from char ** .这与char **非常不同。

If you have an array then used in expressions it is implicitly converted (with rare exceptions) to a pointer to its first element.如果您有一个数组,然后在表达式中使用它,它会被隐式转换(极少数例外)为指向其第一个元素的指针。

On the other hand, a function parameter having an array type is adjusted by the compiler to pointer to the array element type.另一方面,具有数组类型的function参数被编译器调整为指向数组元素类型的指针。

For example if you declared a function like例如,如果您声明了 function 之类的

char* fcn( char *para[] );

then the compiler will adjust the function declaration the following way然后编译器将通过以下方式调整 function 声明

char* fcn( char **para );

You even may include the both declarations in your program though the compiler can issue a message that there are redundant declarations.您甚至可以在程序中包含这两个声明,尽管编译器会发出一条消息,指出存在冗余声明。

So if you have a function like this因此,如果您有这样的 function

char* fcn( char *para[] );

and in the caller you have an array like this在调用者中你有一个这样的数组

char *arg[] = {"XX", "YY", "ZZ"};

then the function parameter and the array declared in the caller have array types with the same element type.那么 function 参数和调用者中声明的数组具有相同元素类型的数组类型。 So you may call the function like所以你可以像这样调用 function

fcn( arg );

The array used as the argument is implicitly converted to a pointer to its first element.用作参数的数组被隐式转换为指向其第一个元素的指针。

The type of array elements is char * .数组元素的类型是char * So a pointer to an element of the array will have the type char ** .因此,指向数组元素的指针将具有char **类型。

That is this function call就是这个 function 调用

fcn( arg );

is equivalent to相当于

fcn( &arg[0] );

Dereferencing the pointer you will get the first element of the array having the type char * .取消引用指针,您将获得类型为char *的数组的第一个元素。

If you will write the expression如果你会写表达式

&arg

then the type of the expression will be char * ( * )[3] .那么表达式的类型将是char * ( * )[3] Dereferencing the pointer you will get the original array of the type char *[3] .取消引用指针,您将获得char *[3]类型的原始数组。

Here is a demonstrative program这是一个演示程序

#include <stdio.h>

void f( char **arg )
{
    printf( "sizeof( *arg ) = %zu\n", sizeof( *arg ) );
}

void g( char * ( *arg )[3] )
{
    printf( "sizeof( *arg ) = %zu\n", sizeof( *arg ) );
}

int main(void) 
{
    char * arg[] = { "XX", "YY", "ZZ" };
    
    f( arg );
    g( &arg );
    
    return 0;
}

The program output is程序 output 是

sizeof( *arg ) = 8
sizeof( *arg ) = 24

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM