Matthew Young
Matthew Young

Reputation: 151

gcc saying "assignment from incompatible pointer type [enabled by default]

Ok 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 don't know why.

here's 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 create arrays function:

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 its header

char * createArray(int);

I have no idea how to get this to go away. I've tried taking away and adding asteriks and changing from one equal signs to two, but it's not working. 2nd year cs student, first year in C. Any help would be appreciated a million times over. Thanks!

Upvotes: 5

Views: 19733

Answers (3)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

You are not counting 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 count is different, you are doing it wrong.

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

The dictionary is char**. Why? It's an array (the first *) of strings(the second *). So it cannot be char or char* or char***. So on the right hand side of the assignment you need a char** too, so createArray must return char**, and a must be a char**, and inside the sizeof inside the malloc you need N-1=1 star.

Upvotes: 3

Your createArrray has the wrong 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 change the declaration in your header file accordingly:

char** createArray(unsigned);

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

NB: there is no point in having count declared as int (morally, it cannot be negative).

Upvotes: 5

MByD
MByD

Reputation: 137362

Your createArray functions is declared and implement with a mistake. You need an array of char pointers, which 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;
}

Upvotes: 6

Related Questions