Gcc say "assignment from an incompatible pointer type [enabled by default]

So, I keep getting this error:

$ gcc -Wall -g translate.c support.c scanner.c -o translate translate.c: In function 'main': translate.c:22:16: warning: assignment from incompatible pointer type [enabled by default] dictionary = createArray(count); ^ support.c: In function 'readTokens': support.c:66:18: warning: assignment from incompatible pointer type [enabled by default] a[count] = token; ^ 

and I do not know why.

here is my main function:

 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "support.h" int main(int argc, char** argv) { int i; int count; char** dictionary; if (argc != 3) { printf("need two arguments!\n"); exit(-1); } count = countTokens(argv[1]); printf("there are %d tokens and strings\n", count); dictionary = createArray(count); readTokens(argv[1], dictionary); printf("The dictionary:\n"); for (i = 0; i < count; ++i) { printf("%s\n", dictionary[i]); } return 0; } 

and my function to create arrays:

 char* createArray(int count) { char* a; a = malloc(sizeof(char*) * count); if (a == 0) { fprintf(stderr, "memory allocation failed\n"); exit(1); } return a; } 

and title

char * createArray(int);

I have no idea how to get this to go. I tried to take and add asterics and go from one equal sign to two, but it does not work. 2nd year student cs, first year in C. Any help would be appreciated a million times. Thanks!

+5
source share
3 answers

Your createArray functions createArray declared and implemented with an error. You need an array of char pointers that is of type ( char ** ), so create and return such an array:

 char** createArray(int count) { char** a; a = malloc(sizeof(char*) * count); if (a == 0) { fprintf(stderr, "memory allocation failed\n"); exit(1); } return a; } 
+6
source

Your createArrray has an invalid signature. Try instead

 char** createArray(unsigned count) { char** a = malloc(sizeof(char*) * count); if (a == NULL) { perror("createArray"); exit(EXIT_FAILURE); } return a; } 

Of course, modify the declaration in your header file accordingly:

 char** createArray(unsigned); 

By the way, you are right to compile with gcc -Wall -g . Now try to run your program step by step in the gdb debugger.

NB: it makes no sense to specify count as int (morally, it cannot be negative).

+5
source

You do not count your stars.

 Foo* foo = // 1 star malloc(sizeof(Foo)); // 0 stars Foo** foo = // 2 stars malloc(sizeof(Foo*)); // 1 star Foo******* foo = // N stars malloc(sizeof(Foo******)); // N-1 stars 

If your score is different, you are doing it wrong.

Of course, this is just a security check. You need to understand what each * in your code does.

Dictionary char** . What for? This is an array (first * ) of rows (second * ). So it cannot be char or char* or char*** . Therefore, on the right side of the task, you need char** , so createArray should return char** , and a should be char** , and inside sizeof inside malloc you need N-1 = 1 star.

+3
source

Source: https://habr.com/ru/post/1202175/


All Articles