好的,所以我一直收到这个错误:
$ 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;
^
Run Code Online (Sandbox Code Playgroud)
而且我不知道为什么.
这是我的主要功能:
#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;
}
Run Code Online (Sandbox Code Playgroud)
和我的创建数组函数:
char* createArray(int count) {
char* a;
a = malloc(sizeof(char*) * count);
if (a == 0) {
fprintf(stderr, "memory allocation failed\n");
exit(1);
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
和它的标题
char * createArray(int);
我不知道如何让它消失.我试过带走并添加星号并从一个等号改为两个,但它不起作用.第二年cs学生,第一年在C.任何帮助将被赞赏一百万次.谢谢!
您的createArray函数被声明并执行时出错.你需要一个char指针数组,它是type(char **),所以创建并返回这样一个数组:
char** createArray(int count) {
char** a;
a = malloc(sizeof(char*) * count);
if (a == 0) {
fprintf(stderr, "memory allocation failed\n");
exit(1);
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
你createArrray的签名错了.试试吧
char** createArray(unsigned count) {
char** a = malloc(sizeof(char*) * count);
if (a == NULL) {
perror("createArray"); exit(EXIT_FAILURE);
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
当然,相应地更改头文件中的声明:
char** createArray(unsigned);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你是正确的编译gcc -Wall -g.现在尝试在gdb调试器中逐步运行程序.
注意:count宣称没有意义int(道德上,它不能是否定的).