Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a char pointer array 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'm trying to pass char and char pointer array to a function, but the above code results in 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'm trying to understand what the problem is.

like image 709
user1436057 Avatar asked Dec 09 '22 22:12

user1436057


1 Answers

First of all, you should include the necessary header files. For strcmp you need <string.h>, for malloc <malloc.h>. Also you need to at least declare test before main. If you do this you'll 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 indicates that test() should have a char * as first argument. All in all 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

Please notice that strcmp works with null-terminated byte strings. If neither s1 nor s2 contain a null byte the call in test will result in a segmentation fault:

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

Either make sure that both contain a 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 allocation of memory is wrong. array is a char**. You allocate memory for *array which is itself a char*. You never allocate memory for this specific pointer, you're missing array[0] = malloc(n*sizeof(**array)):

array = malloc(sizeof(*array));
*array = malloc(n * sizeof(**array));
like image 61
Zeta Avatar answered Dec 27 '22 17:12

Zeta