[英]Error - Subscripted value is neither array nor pointer nor vector in C
[英]error: subscripted value is neither array nor pointer nor vector in C
the_project.c:73:22: error: subscripted value is neither array nor pointer nor vector
它給出了上面的錯誤,下面是第73行。
customer_table[my_id][3] = worker_no;
我聲明了全局數組如下
int *customer_table; //All the info about the customer
此代碼行不在main函數中。 我主要為此全局數組分配內存。 這可能導致此問題嗎?
您正在聲明一個pointer-to-int
的pointer-to-int
。 因此cutomer_table[x]
是一個int,而不是一個指針。 如果需要二維動態分配的數組,則需要:
int **customer_table;
而且您在分配時需要非常小心。
(有關示例,請參見例如2D char數組的動態內存 。)
問題是customer_table[my_id]
不是指針或數組,因此不能在其上使用[]
。
注意,由於customer_table
是一個指針,因此使用[]
的第一個取消引用是可以的。 一旦應用了第一個[]
它就會變成一個int
。
也許您真正想要使用的是
int **customer_table;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.