2016-04-30 110 views
0

我正在學習C,並試圖爲實踐創建一個ArrayList當我嘗試編譯此代碼時,出現了很多錯誤,但我想不通爲什麼我收到這樣的:C - 錯誤:期望的標識符或'('之前的'void'

TEST.C:12:13:錯誤:預期標識符或 '(' 前 '無效' 結構的init(無效);

有沒有很多關於谷歌這個錯誤連接到無效,所以任何幫助將不勝感激,謝謝!

#include <stdio.h> 
    #include <stdlib.h> 

    struct ArrayList { 
    int size; 
    int typeSize; 
    int index; 
    int array[]; 
}; 

struct init(void); 

void add(struct list, int x); 

int get(struct list, int x); 


int main() { 
    struct ArrayList list = init(); 
    add(list, 4); 
    printf("%d", get(list, 0)); 
    return 0; 
} 


struct init(void) { 
    ArrayList this; 
    this.size = 0; 
    this.index = 0; 
    return this; 
} 

void add(struct list, int x) { 
    list->array[size] = x; 
    size++; 
} 

int get(struct list, int index) { 
    return list->array[index]; 
} 
+0

順便說一句'list-> array [size] = x; – BLUEPIXY

+0

並用'struct ArrayList * list'替換'struct list' – BLUEPIXY

回答

2

Th e結構名稱丟失。

struct ArrayList init(void); 

struct ArrayList init(void) { 
    ArrayList this; 
    this.size = 0; 
    this.index = 0; 
    return this; 
} 

另一個問題,array是不是在你的情況下進行初始化,這將是不確定的行爲在addget使用它:

struct ArrayList init(void) { 
    ArrayList this; 
    this.size = 0; 
    this.index = 0; 
    return this; 
} 

在這種情況下,靈活的陣列成員array不相關與任何存儲相比,最好這樣做:

struct ArrayList* init(void) { 
    ArrayList *this = malloc(sizeof(struct ArrayList) + sizeof(int[NUM])); 
    this->size = NUM; 
    this->index = 0; 
    return this; 
} 

addget功能需要太固定的:

void add(struct ArrayList *list, int x) { 
    list->array[list->index++] = x; 
} 

int get(struct ArrayList *list, int index) { 
    return list->array[index]; 
} 
+0

'this.array = malloc(SIZE * sizeof(int));'無法編譯。 'this.array'是靈活的數組,不是指針。 – BLUEPIXY

+1

@BLUEPIXY你是對的,整個結構需要爲數組分配存儲空間,'struct ArrayList list * = malloc(sizeof(struct ArrayList)+ sizeof(int [size]));'但是OP的init '返回一個結構體,而不是一個指針,這會讓這個變得困難。 – fluter

+0

更正了該帖子。 – fluter

0

struct init(void); - 它看起來像沒有參數的函數返回一個struct。但是什麼struct?可能struct ArrayList

對於其餘外觀struct而言,沒有struct類型的名稱也是如此。

4
struct init(void); 

這是一個語法錯誤。

鑑於類型聲明struct ArrayList { ... },類型的名稱是struct ArrayList。如果init應該返回ArrayList結構,它需要被聲明爲:

struct ArrayList init(void); 

您還在init函數的定義有一個錯誤。此聲明:

ArrayList this; 

需求是:

struct ArrayList this; 

(作爲一個風格上看,你可能會考慮使用比this以外的名稱在C++中,this是一個關鍵詞,你使用。作爲一種方式的標識符與它的C++的意思不一致,這是完全合法的,但它可能會造成一些混亂。我建議稱之爲result。)

1
struct init(void); 

struct本身不是一個足夠的類型。你可能想使用這樣的代替:

struct ArrayList init(void); 
0

你在你的數據類型,有這麼多的錯誤.. 結構是不是數據類型。檢查這個。我已經改變了你的代碼。記住如果你打算使用指針,你至少應該通過地址

#include <stdio.h> 
#include <stdlib.h> 

struct ArrayList { 
int size; 
int typeSize; 
int index; 
int *array; 
}; 

struct ArrayList init(); 

void add(struct ArrayList* list, int x); 

int get(struct ArrayList* list, int x); 


int main() { 
    struct ArrayList list = init(); 
    add(&list, 4); 
    printf("%d", get(&list, 0)); 
    return 0; 
} 


struct ArrayList init() { 
    struct ArrayList this; 
    this.size = 0; 
    this.index = 0; 
    this.array = (int*)malloc(sizeof(int) * /*numOfelementsToBeInserted*/); 
    return this; 
} 

void add(struct ArrayList *list, int x) { 
    list->array[list->size] = x; 
    list->size++; 
} 

int get(struct ArrayList *list, int index) { 
    return list->array[index]; 
} 
+0

我修改了我的代碼,它和你在這裏發佈的內容類似,有一件事我改變了,我把struct []放到了int *數組中,我得到了預期的輸出,但是我也得到了「 test.exe停止工作「錯誤,並通過調試,我發現這發生在你的第一行添加。任何想法爲什麼? –

+0

如果你改變你的**結構從array []到int * array **你仍然需要分配的空間,用元素來驗證 – jnapor

+0

我加了這個:struct ArrayList * newList = malloc(sizeof(struct ArrayList)+ sizeof(int)* 20);我相信這會爲int *分配空間,正確嗎? –

相關問題