简体   繁体   English

正确的“地址”字符指针数组的C语法?

[英]Correct C syntax for “address of” character pointer array?

I need to pass into a function the address of a character pointer array, what would be the correct function declaration/prototype syntax for that? 我需要将一个字符指针数组的地址传递给函数,对于那个函数声明/原型语法是什么?

I tried: 我试过了:

    void myFunc(char &*a[]);

But get an expected ; 但得到一个预期; , or ) before & error. ,或)之前和错误。

I also tried: 我也尝试过:

    void myFunc(char **a);

Since the pointer I would be passing in is indeed a pointer to a pointer, but I get yet another error. 因为我要传入的指针确实是一个指向指针的指针,但是我又得到了另一个错误。 This time the error has to do with: expected char ** , but got char *[] 这次错误与预期的char ** ,但得到了char *[]

Or something like that. 或类似的东西。 I have since attempted other solutions so I not remember exactly the error. 我已经尝试过其他解决方案,所以我不记得确切的错误。

Any suggestions? 有什么建议么?

Assuming you have an array declared as 假设你有一个声明为的数组

char *a[N];

and you want to pass a pointer to the array, as in 并且您想要传递指向数组的指针,如

foo( &a );

then the prototype for foo needs to be 那么foo的原型需要

void foo( char *(*aptr)[N] );

Note that in this case, the size of the array must be declared; 请注意,在这种情况下, 必须声明数组的大小; a pointer to an N-element array is a different type from a pointer to an M-element array. 指向N元素数组的指针与指向M元素数组的指针的类型不同。

Normally, you don't want to do this; 通常,你不想这样做; instead, you would normally just pass the array expression like so: 相反,你通常会像这样传递数组表达式:

foo ( a );

and the corresponding prototype would be: 相应的原型将是:

void foo ( char **aptr );

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T " will be converted ("decay") to an expression of type "pointer to T ", and the value of the expression will be the address of the first element of the array. 除了当它是的操作数sizeof或一元&运营商,或者是一个字符串被用来初始化一个声明另一个数组,类型“的N元件阵列的表达T将被转换(“衰变”)”,以表达式“指向T指针”,表达式的值将是数组的第一个元素的地址。

Updated Answer For Modified Problem Statement 修改后的问题陈述的更新答案

Given what you have said in comments, there is no need to pass a pointer to an array. 鉴于您在注释中所说的内容,不需要传递指向数组的指针。 You can simply pass a pointer to the first element of the array. 您只需将指针传递给数组的第一个元素即可。 Such a pointer suffices because the remaining elements of the array are obviously located after the first element. 这样的指针就足够了,因为数组的其余元素显然位于第一个元素之后。

To write a function that sets pointers in an array of pointers to char , do this: 要编写一个在指向char数组的指针数组中设置指针的函数,请执行以下操作:

void MyFunction(int NumberToSet, char *Pointers[])
{
    for (int i = 0; i < NumberToSet; ++i)
    {
        Pointers[i] = SomeString;
    }
}

In the above, SomeString must have type “pointer to char ”. 在上面, SomeString必须具有类型“指向char指针”。 This could be a string, such as "Hello" , or an array of char (which is automatically converted to a pointer to char ), or some identifier x that has been declared as char *x (and has been initialized or assigned), for example. 这可以是一个字符串,例如"Hello" ,或者一个char数组(它自动转换为指向char的指针),或者一些标识符x ,它已被声明为char *x (并且已被初始化或分配),例如。

To use this function, call it like this: 要使用此功能,请按以下方式调用:

char *MyArrayOfPointers[SomeNumber];
MyFunction(NumberOfPointersIWantToSet, MyArrayOfPointers);

Original Answer 原始答案

In most cases, to pass an array of pointers to char to a function, it suffices to pass the address of the first element. 在大多数情况下,要将char的指针数组传递给函数,只需传递第一个元素的地址即可。 In this case, you would use either of these (they are equivalent): 在这种情况下,您将使用其中任何一个(它们是等效的):

void myFunc(char **a)
void myFunc(char *a[])

If you truly want to pass the address of the array, you would use: 如果你真的想传递数组的地址,你会使用:

void myFunc(char *(*a)[])

In this case, the type of a is incomplete, since the dimension is missing. 在这种情况下,类型a是不完整的,因为尺寸缺失。 Depending on what you intend to do with a , you may need to provide the dimension in the declaration. 根据您打算如何处理a ,您可能需要在声明中提供维度。

When calling myFunc and passing it some array declared as char *array[N]; 当调用myFunc并传递一些声明为char *array[N]; , you would pass it, in the former case, as myFunc(array) and, in the latter case, as myFunc(&array) . ,在前一种情况下,你将它传递给myFunc(array) ,在后一种情况下,传递给myFunc(&array)

try this as a function definition void myFunc(char *a[]) or void myFunc(char **a) then use it this way : 尝试将此作为函数定义void myFunc(char *a[])void myFunc(char **a)然后以这种方式使用它:

char *arr[20];
myFunc(arr);

Declaration 宣言

void myFunc(char &*a[]);  

is not a valid C syntax. 不是有效的C语法。
To pass the address of character pointer arrays , use this instead 要传递字符指针数组地址 ,请改用它

void myFunc(char *(*a)[]);   

*(*a)[] in the above function declares a as pointer to array of pointers to char s . *(*a)[]在上面的函数中声明a指向char的指针数组的指针 Must note that a has an incompatible type. 必须注意a具有不兼容的类型。 A suffix is needed in [] to make it complete. []需要后缀才能完成。

Ok you are almost on the right path. 好的,你几乎走在正确的道路上。 void myFunc(char *a[]);

Example

void fun(char *a[]){
    printf("%s",*a);    //for accessing the next element do a+1
    }

int main(void) {
    char *x[3];
    x[0]="abcd";
    fun(x);   // here you are passing the address first array element 
    return 0;

DEMO DEMO

First of all, C is in general a "pass by reference" language. 首先,C通常是“通过引用传递”语言。 Some data items such as integers, floats, and single characters can be passed by value. 某些数据项(如整数,浮点数和单个字符)可以按值传递。 But, arrays of those same data types are ALWAYS passed by reference. 但是,那些相同数据类型的数组总是通过引用传递。

Thus, when you say "I need to pass into a function the address of a character pointer array" then simply declare an array of character pointers in your function prototype as: 因此,当你说“我需要将函数传递给函数时,字符指针数组的地址”然后只需在函数原型中声明一个字符指针数组:

void myFunc(char *a[]);

Thus, char * declares a char pointer and a[] defines an array of them. 因此, char *声明了一个char指针, a[]定义了它们的数组。 To check this declaration refer to: http://www.cdecl.org/ which parses this expression as a "declare a as array of pointer to char". 要检查此声明,请参阅: http//www.cdecl.org/ ,它将此表达式解析为“声明一个指向char的指针数组”。

The technical point is that the * binds with char rather than with a[] . 技术要点是*char绑定而不是与a[]绑定。

So, C will pass a pointer to the data structure that you have declared. 因此,C将传递指向您声明的数据结构的指针。 A discussion on this topic could delve into double pointers but for this question such a discussion is probably off topic. 关于这个主题的讨论可以深入研究双指针,但对于这个问题,这样的讨论可能不是主题。

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

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