预计'为const char *',但参数类型是在C'的char **' [英] expected ‘const char *’ but argument is of type ‘char **’ in C

查看:2088
本文介绍了预计'为const char *',但参数类型是在C'的char **'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个搜索数组,寻找指定key.The参数n指定数组的有效尺寸,它必须根据如发现由strcmp.If规定的密钥字典顺序进行排序的函数,该函数返回数组中表示该数据的关键appears.So,它可以返回substring.However指数的指数,它来了,我不能修复with.Please帮助两个错误。

<$p$p><$c$c>jiebin@jiebin-ThinkPad-Edge-E530:~/Program_C/programming_abstractions_in_c/FindStringInSortedArray$ GCC FindStringInSortedArray.c -o FindStringInSortedArray
FindStringInSortedArray.c:在函数'FindStringInSortedArray:
FindStringInSortedArray.c:7:3:警告:从兼容的指针类型传递的'strlen的'参数1 [默认启用]
/usr/include/string.h:399:15:注:应为为const char *',但参数的类型为字符**
张介宾@张介宾-的ThinkPad边-E530:〜/ Program_C / programming_abstractions_in_c / FindStringInSortedArray $

这是我的code:

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;INT FindStringInSortedArray(字符*键,字符* []数组,INT N)
{
  INT中旬,CMP;
  INT低= strlen的(阵列)-n;
  INT高= N;
  如果(低&GT;高)回报(-1);
  中期=(低+高)/ 2;
  CMP = STRCMP(键,数组[MID]);
  如果(CMP == 0)返回(中);
  如果(CMP℃,){
    返回(FindStringInSortedArray(标号,阵列中,n / 2));
   }其他{
    回报(FindStringInSortedArray(键,阵列+ N / 2,N));
   }
}诠释的main()
{
  炭键[2] =AB;
  的char *数组[10] = {AB,BC,CD,德,EF,FG,GH,喜,IJ,JK};
  INT测试= FindStringInSortedArray(键,阵列,10);
  的printf(结果数:%d \\ n,测试);
  返回0;
}


解决方案

INT低= strlen的(阵列)-n; 是错误的。传递数组大小不同的参数,如:

  INT FindStringInSortedArray(字符*键,字符* []数组,整数N,INT ARRAYSIZE)

因为数组衰变为函数指针。 strlen的宣言的形式为

 的strlen(为const char *)

和你逝去的 *数组[] 其类型衰减到的char *

在C99有三个,其中数组名不衰变成指针第一要素的基本情况:


  1. 在它的&放大器的参数; (地址)的运营商


  2. 在它的的sizeof 运营商的说法。


  3. 在它的文本类型的字符串的char [N + 1] 或宽字符串文字类型的 wchar_t的[N + 1 ] N 是字符串的长度),用来初始化数组,如字符海峡[] =富; wchar_t的WSTR [] = L富;


在C11,新推出的 alignof 运营商不让其数组参数衰变成一个指针无论是。

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.

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;
}

解决方案

int low=strlen(array)-n; is wrong. Pass the array size as different parameter like:

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 (const char*)

And you are passing *array[] whose type decays to char * *.

In C99 there are three fundamental cases where array name doesn't decay into pointers to first elements:

  1. when it's the argument of the & (address-of) operator.

  2. when it's the argument of the sizeof operator.

  3. When it's a string literal of type char [N + 1] or a wide string literal of type wchar_t [N + 1] (N is the length of the string) which is used to initialize an array, as in char str[] = "foo"; or wchar_t wstr[] = L"foo";.

In C11, the newly introduced alignof operator doesn't let its array argument decay into a pointer either.

这篇关于预计'为const char *',但参数类型是在C'的char **'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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