这是我在C大学教授的大学课程中的一个项目。我对C和编程非常陌生,而且我有时会遇到一些错误。
我遇到错误问题
level1.c:23: error: expected expression before ‘int’
和
level1.c:23: warning: assignment makes pointer from integer without a cast
我花了很长时间尝试不同的东西来修复错误,但我无法弄明白。 这是它的源代码。
int main(int argc,char **argv)
{
int i;
int count;
char *dictionary;
if (argc != 3)
{
printf("need two arguments!\n");
exit(-1);
}
count = readALLTokens(argv[1]);
printf("there are %d tokens and strings\n",count);
dictionary = memalloc(int *count); /* ERROR ON THIS LINE */
arrayfill(argv[1]);
printf("THE DICIONARY...\n");
for (i = 0; i < count; ++i)
{
printf("%d\n",dictionary[i]);
}
return 0;
}
这是它在具有所有其他相关功能的另一个文件中引用的功能。
int readALLTokens(char *);
int count = 0;
int readALLTokens(char *dictionary)
{
FILE *fp;
char *token;
fp = fopen(dictionary,"r");
if (fp == 0)
{
fprintf(stderr,"file %s could not be opened for reading\n",dictionary);
exit(1);
}
token = readToken(fp);
while (!feof(fp))
{
printf("%s\n",token);
++count;
free(token);
token = readToken(fp);
}
fclose(fp);
return count;
}
char *a[10];
int memalloc(int *count)
{
*a = malloc(sizeof(count));
return 0;
}
void arrayfill(char *dictionary)
{
FILE *fp;
fp = fopen(dictionary,"r");
int t = 0;
char *token;
token = readToken(fp);
while (!feof(fp))
{
fscanf(fp,"%s",*(a + t));
++t;
free(token);
token = readToken(fp);
}
fclose(fp);
return;
}
到目前为止的想法是它应该读取字典文件,创建一个数组并为其分配适当数量的内存然后将字典文件读入数组,以便它可以用于比较另一个文件和使用字典文件中的字符串“翻译”它。 我不确定我的代码有多少是正确的,但似乎能够做到我需要的东西到目前为止。
答案 0 :(得分:2)
第一件事:您的代码会输出大量警告。其中许多与您在调用它们之前没有函数原型这一事实有关。您应认真解决此问题。
其次:要传递指向变量的指针,请使用&
运算符。如:
dictionary = memalloc(&count);