2014-10-05 39 views
0

這是針對C中教授的大學課程中的一個項目。我對C語言和編程非常陌生,有時我會遇到一些錯誤。錯誤:在'int'之前的預期表達式

我在與錯誤的問題

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; 
} 

的想法到目前爲止是,它應該讀出的詞典文件,創建一個數組和分配的存儲器中的適當的量爲它然後讀取字典文件到所述陣列,使得其可用於比較到另一個文件並使用字典文件中的字符串「翻譯」它。 我不知道我的代碼有多少是正確的,但它似乎能夠做到我需要它到這一點。

+1

安置自己確切的代碼 - 我想也許你留下來爲簡潔一些行#include,但這些都是這個問題 – 2014-10-05 02:53:59

+1

你能描述(英文)你所期望的「字典= memalloc(INT重要*計數);」去做;還有「memalloc」應該做什麼? – 2014-10-05 02:55:31

+0

我實際上並不記得「dictionary = memalloc(int * count);」應該這樣做。我前幾天寫了這個項目,當時我開始了這個項目。現在我看到它了,我並不完全確定我原先想要做什麼。我在這一點上需要做的是爲數組「a []」分配內存,這樣我就可以用arrayfill函數中的字典中的令牌來填充內存。此外,我確實有頭文件引用所有這些功能。我早先忘記提及。 – 2014-10-05 03:13:02

回答

2

第一件事第一件事:您的代碼輸出大量的警告。其中很多涉及到事實,你沒有你的功能原型之前,你打電話給他們。你應該認真解決這個問題。

其次:要將指針傳遞給變量,請使用&運算符。如:

dictionary = memalloc(&count); 
+2

這個問題比這個更深入,'memalloc'函數不需要改變'count',但它完全被破壞 – 2014-10-05 02:54:56

相關問題