C语言 预期为“const char *”,但参数在 C 中属于“char **”类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20213066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expected ‘const char *’ but argument is of type ‘char **’ in C
提问by luojiebin
I am trying to write a function which searches the array looking for the specified key.The argument n specifies the effective size of the array,which must be sorted according to the lexicographic order imposed by strcmp.If the key if found,the function returns the index in the array at which that key appears.So ,it can return the index of the substring.However,it come two errors which I can't fix with.Please help.
我正在尝试编写一个函数来搜索数组以查找指定的键。参数 n 指定数组的有效大小,必须根据 strcmp 强加的字典顺序进行排序。如果找到键,则函数返回该键出现的数组中的索引。所以,它可以返回子字符串的索引。但是,它出现了两个我无法修复的错误。请帮忙。
jiebin@jiebin-ThinkPad-Edge-E530:~/Program_C/programming_abstractions_in_c/FindStringInSortedArray$ gcc FindStringInSortedArray.c -o FindStringInSortedArray
FindStringInSortedArray.c: In function ‘FindStringInSortedArray':
FindStringInSortedArray.c:7:3: warning: passing argument 1 of ‘strlen' from incompatible pointer type [enabled by default]
/usr/include/string.h:399:15: note: expected ‘const char *' but argument is of type ‘char **'
jiebin@jiebin-ThinkPad-Edge-E530:~/Program_C/programming_abstractions_in_c/FindStringInSortedArray$
This is my code:
这是我的代码:
#include<stdio.h>
#include<string.h>
int FindStringInSortedArray(char *key,char *array[],int n)
{
int mid,cmp;
int low=strlen(array)-n;
int high=n;
if(low > high) return(-1);
mid = (low+high)/2;
cmp = strcmp(key,array[mid]);
if(cmp==0) return(mid);
if(cmp<0){
return(FindStringInSortedArray(key,array,n/2));
}else{
return(FindStringInSortedArray(key,array+n/2,n));
}
}
int main()
{
char key[2]="ab";
char *array[10]={"ab","bc","cd","de","ef","fg","gh","hi","ij","jk"};
int test=FindStringInSortedArray(key,array,10);
printf("Result:%d\n",test);
return 0;
}
回答by Sadique
int low=strlen(array)-n;is wrong. Pass the array size as different parameter like:
int low=strlen(array)-n;是错的。将数组大小作为不同的参数传递,例如:
int FindStringInSortedArray(char *key,char *array[],int n, int arraysize)
Since arrays decay into pointers in functions. Declaration of strlen is of the form
由于数组衰减为函数中的指针。strlen 的声明形式为
strlen (const char*)
And you are passing *array[]whose type decays to char * *.
并且您正在传递*array[]类型衰减为char * *.
In C99 there are three fundamental cases where array name doesn't decay into pointers to first elements:
在 C99 中,存在三种基本情况,其中数组名称不会衰减为指向第一个元素的指针:
when it's the argument of the
&(address-of) operator.when it's the argument of the
sizeofoperator.When it's a string literal of type
char [N + 1]or a wide string literal of typewchar_t [N + 1](Nis the length of the string) which is used to initialize an array, as inchar str[] = "foo";orwchar_t wstr[] = L"foo";.
当它是
&(address-of)运算符的参数时。当它是
sizeof运算符的参数时。当它是用于初始化数组的字符串文字类型
char [N + 1]或宽字符串文字类型wchar_t [N + 1](N是字符串的长度)时,如char str[] = "foo";或wchar_t wstr[] = L"foo";。
In C11, the newly introduced alignofoperator doesn't let its array argument decay into a pointer either.
在 C11 中,新引入的alignof运算符也不会让其数组参数衰减为指针。
回答by Fabien
When you call strlen, it is expecting a char*(i.e. a string) as an argument, but you provide it with arraywhich is a char**(i.e. an array of strings).
当您调用 时strlen,它期望一个char*(即一个字符串)作为一个参数,但是您向它提供arraywhich is a char**(即一个字符串数组)。
What you want is the size of the array, i guess. There is no way to know it, in C. The only way is to pass the size of the array as an argument :
我想你想要的是数组的大小。在 C 中没有办法知道它。唯一的方法是将数组的大小作为参数传递:
int FindStringInSortedArray(char *key,char *array[],int n, int len)

