Pass an array of char pointers to a function in C?

I have the following code:

int main(){ char **array; char a[5]; int n = 5; array = malloc(n *sizeof *array); /*Some code to assign array values*/ test(a, array); return 0; } int test(char s1, char **s2){ if(strcmp(s1, s2[0]) != 0) return 1; return 0; } 

I am trying to pass an array of char and char pointers to a function, but the above code leads to the following errors and warnings:

  temp.c: In function 'main':
 temp.c: 6: 5: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
 temp.c: 6: 13: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
 temp.c: 10: 5: warning: implicit declaration of function 'test' [-Wimplicit-function-declaration]
 temp.c: At top level:
 temp.c: 15: 5: error: conflicting types for 'test'
 temp.c: 15: 1: note: an argument type that has a default promotion can't match an empty parameter name list declaration
 temp.c: 10: 5: note: previous implicit declaration of 'test' was here
 temp.c: In function 'test':
 temp.c: 16: 5: warning: implicit declaration of function 'strcmp' [-Wimplicit-function-declaration] 

I am trying to understand what the problem is.

+4
source share
4 answers

First of all, you must include the necessary header files. For strcmp you need <string.h> , for malloc <malloc.h> . You also need to at least declare a test in front of the main one. If you do this, you will notice the following error:

  temp.c: In function 'test':
 temp.c: 20: 5: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]
 /usr/include/string.h:143:12: note: expected 'const char *' but argument is of type 'char'

This means that test() must have char * as the first argument. In general, your code should look like this:

 #include <string.h> /* for strcmp */ #include <malloc.h> /* for malloc */ int test(char*,char**); /* added declaration */ int main(){ char **array; char a[5]; int n = 5; array = malloc(sizeof(*array)); array[0] = malloc(n * sizeof(**array)); /*Some code to assign array values*/ test(a, array); free(*array); /* free the not longer needed memory */ free(array); return 0; } int test(char * s1, char **s2){ /* changed to char* */ if(strcmp(s1, s2[0]) != 0) /* have a look at the comment after the code */ return 1; return 0; } 

Edit

Note that strcmp works with null terminated byte strings. If neither s1 nor s2 contain zero bytes, a call to test will result in a segmentation error:

  [1] 14940 segmentation fault (core dumped) ./a.out 

Either make sure that both contain the null byte '\0' , or use strncmp and change the signature of test :

 int test(char * s1, char **s2, unsigned count){ if(strncmp(s1, s2[0], count) != 0) return 1; return 0; } /* don' forget to change the declaration to int test(char*,char**,unsigned) and call it with test(a,array,min(sizeof(a),n)) */ 

Also your memory allocation is wrong. array is char** . You allocate memory for *array , which itself is char* . You never allocate memory for this particular pointer, you are missing array[0] = malloc(n*sizeof(**array)) :

 array = malloc(sizeof(*array)); *array = malloc(n * sizeof(**array)); 
+7
source

Error 1

 temp.c:6:13: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default] 

Did you mean this?

 array = malloc(n * sizeof(*array)); 

Error 2

 temp.c:15:5: error: conflicting types for 'test' temp.c:15:1: note: an argument type that has a default promotion can't match an empty parameter name list declaration temp.c:10:5: note: previous implicit declaration of 'test' was here 

You pass the address of the first element of array a :

  test(a, array); 

Thus, the signature of the function should be:

 int test(char* s1, char** s2) 
+3
source

You have a few issues. Firstly, the prototype is wrong. The data type for a decays to a char pointer when passing the function, so you need to:

 int test (char* s1, char** s2) { ... } 

However, even if you fix it, the test declaration is not in scope when you use it for the first time. You must either provide a prototype:

 int test (char* s1, char** s2); 

to main or just move the whole definition (function) to main .

Also, don't forget to #include headers string.h and stdlib.h so that prototypes for strcmp and malloc .

+3
source

When you pass a char array to your function, the argument breaks into a pointer. Change function arguments to

  int test(char* s1, char **s2); ^ ^ 

and your code should at least compile

+1
source

All Articles