(作業問題) 我只是學習C,和我正在一個程序,從文件中讀取數據,創建數據的路由器,並把指針在大小255的數組中的路由器,但我不斷收到在哪裏我試圖將它們添加到陣列下標值既不是數組也不指針也不矢量在數組索引
#define ARRAY_SIZE 255
struct router routers[ARRAY_SIZE] = {0};
int main(int argc, char *argv[]){
unsigned char id;
char name[32];
struct router *new_router;
if(argc == 2){
//reads file with fread
//setting id and name which prints out as expected
new_router = make_router(id, name); //initialising method that returns a router pointer
routers[new_router->id] = new_router;
//error occurs here, at [new_router->id]. Have also tried just using id
}
}
我已經搜查了很多線程具有相同錯誤的行標題錯誤消息,但它們都是或者沒有聲明數組的人,或者是建議使用unsigned char作爲索引號嘗試它,這是我已經使用的。希望對此有所瞭解。
struct router{
unsigned char id;
char name[32];
}
struct router* make_router(unsigned char id, char* name){
struct router *r = malloc(sizeof(struct router));
r->id = id;
r->name = name;
return r;
}
'router'的定義在哪裏? –
路由器是一個無符號字符的ID,和一個char *名稱 – Telanore
一個非常基本的結構。如果它是一個基本的,你可以把這個問題以避免像我這樣的評論。還需要'make_router'的定義。 –