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!
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;
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With