简体   繁体   English

警告:参数与原型(C)|不兼容 char [] VS char **的地址

[英]warning: argument is incompatible with prototype (C) | address of char [] V.S. char**

I have a char array. 我有一个char数组。 I take its address and pass it to a function which accepts a double char pointer: 我取其地址并将其传递给一个接受双字符指针的函数:

char result[1024+1];
memset(result, 0, sizeof(result));
executeGetQuery(query, &(result));

Function definition: int executeGetQuery(char * query, char ** queryResultData) 函数定义: int executeGetQuery(char * query, char ** queryResultData)

I get this compile time error 我得到这个编译时错误

warning: argument #2 is incompatible with prototype: 警告:参数#2与原型不兼容:
prototype: pointer to pointer to char : line 1531 prototype:指向char的指针:第1531行
argument : pointer to array[1025] of char 参数:指向char的数组[1025]的指针

I've always passed arrays as pointers in C before. 我以前总是将数组作为指针传递给C。 Why am I getting this error? 为什么我收到此错误? Is my compiler to blame? 我的编译器应该受到责备吗?

EDIT: What is the fix? 编辑:修复是什么?
EDIT2: I want the function to write something to the result array which is why I am passing a char** instead of char*. EDIT2:我希望函数在结果数组中写一些东西,这就是我传递char **而不是char *的原因。 What is then another way, the way, to have a function write to a param which I pass in? 那么,让函数写入我传入的参数的另一种方式是什么?

char** here means the function will write into given memory location, something like: char**这里表示函数将写入给定的内存位置,如:

int executeGetQuery(char * query, char ** queryResultData) {
    char* buffer = malloc( ... );
    /* get the query results into buffer */
    *queryResultData = buffer;
    ...
}

You can't do that with array, its address is not writable. 你不能用数组做,它的地址是不可写的。

What is the fix? 修复是什么?

In this case (you don't need the char ** ) use char * both in the prototype and the calling code 在这种情况下(您不需要char ** )在原型和调用代码中都使用char *

int executeGetQuery(char *query, char *queryResultData);
executeGetQuery(query, result);

I'd also include a size argument for protecting against buffer overflows 我还包括一个用于防止缓冲区溢出的大小参数

int executeGetQuery(char *query, char *queryResultData, size_t len);
executeGetQuery(query, result, sizeof result);

Edit: 编辑:

to change the contents of the array do something like 改变数组的内容做类似的事情

int executeGetQuery(char *query, char *queryResultData, size_t len) {
    queryResultData[0] = 'H';  /* changing */
    queryResultData[1] = 'i';  /* the contents */
    queryResultData[2] = '\0'; /* of the array */
}

You can see the change in the calling function 您可以看到调用函数的更改

executeGetQuery(NULL, result, sizeof result);
printf("result is now %s\n", result); /* should print "Hi" */

&result is not type char ** . &result不是char **类型。 It is of type char (*)[1025] . 它的类型为char (*)[1025]

You have to pass a pointer to a pointer to char to your function: 你必须将指向char的指针传递给你的函数:

char *p;
executeGetQuery(query, &p);

I'm not sure I can explain this 100% understandably, but the compiler is correct. 我不确定我能解释这100%是可以理解的,但编译器是正确的。 A char** is a pointer to a memory location where a char pointer is stored. char**是指向存储char指针的内存位置的指针。

You're not passing in a memory location storing a pointer, but instead try to pass in the address of a char array. 您没有传入存储指针的内存位置,而是尝试传入char数组的地址。 You can manually assign the char array to a char pointer variable and use the address of that variable, but the compiler won't automatically create that temporary variable for you. 您可以手动将char数组分配给char指针变量并使用该变量的地址,但编译器不会自动为您创建该临时变量。

The function probably takes a char** because it will allocate the result buffer for you and use the return value to indicate errors, for example. 该函数可能需要一个char**因为它将为您分配结果缓冲区并使用返回值来指示错误,例如。 Therefore, it will return the buffer by modifying a char -pointer provided by you. 因此,它将通过修改您提供的char -pointer来返回缓冲区。 To do this it needs to access its memory location. 要做到这一点,它需要访问其内存位置。 That's why the function takes a char** . 这就是函数采用char** So to use it correctly, you would need to do: 所以要正确使用它,你需要做:

char *foo;
executeGetQuery(query, &foo); // foo now points to the result buffer

Although you can use arrays in some contexts, where pointers are required in C, they are not the same. 虽然您可以在某些上下文中使用数组,但在C中需要指针,但它们并不相同。 Your example makes it easy to see why: result is already an allocated memory region (most likely on the stack, if it's a local variable), so it can decay into a pointer pointing to its first element, but it cannot be a lvalue, like a pointer can. 你的例子让人很容易理解为什么: result已经是一个已分配的内存区域(最有可能在堆栈上,如果它是局部变量),所以它可以衰减成指向其第一个元素的指针,但它不能是左值,像一个指针可以。

暂无
暂无

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

相关问题 在 C 中,如何修复此警告:格式“%s”需要“char *”类型的参数,但参数 3 的类型为“char (*)[100] - In C, how to fix this warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100] C:警告:格式'%s'需要匹配的'char *'参数 - C: warning: format '%s' expects a matching 'char *' argument 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)” - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’ C 中的警告说“预期为 'char *' 但参数的类型为 'char **'” - Warning in C that says "expected ‘char *’ but argument is of type ‘char **’" C function 原型:\\char *strinv(const char *s); - C function prototype: \\char *strinv(const char *s); “ char”类型的参数与char *类型的参数不兼容 - Argument of type “char” is incompatible with parameter of type char* system() 在 C 中的函数中更改 char* 参数地址 - system() changes char* argument address in a function in C s期望使用char c类型的参数,但是参数2具有类型'int'的警告和错误的返回值 - s expects argument of type char c but argument 2 has type 'int' warning and bad return 关于char **参数的const char **参数警告 - const char** parameter warning about char** argument fopen不兼容的类型char(*)[9]而不是C中的char(*) - fopen incompatible type char (*)[9] instead of char(*) in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM