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 */
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; }
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));
source share