Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected ‘const char *’ but argument is of type ‘char **’ in C

Tags:

arrays

c

pointers

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;
}
like image 465
luojiebin Avatar asked Nov 26 '13 09:11

luojiebin


People also ask

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

What is the difference between const char * and char * const?

The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).

What does const char * means?

const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated.

Can const char * Be Changed?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed.


2 Answers

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.

like image 177
Sadique Avatar answered Nov 10 '22 04:11

Sadique


When you call strlen, it is expecting a char* (i.e. a string) as an argument, but you provide it with array which is a char** (i.e. an array of strings).

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 :

int FindStringInSortedArray(char *key,char *array[],int n, int len)
like image 27
Fabien Avatar answered Nov 10 '22 02:11

Fabien