variably modified 'users' at file scope


1、#define LISTEN_NUM 10    /*The MAX Number Of Users*/
2、const int LISTEN_NUM = 10;

/*UserProfile Struct, to store user's infomation*/
typedef struct {
char username[10];    /*User Name*/
char userip[20];           /*User IP*/
int isOnline;                /*Is Online*/
} UserProfile;

UserProfile users[LISTEN_NUM];//Users

若用2的方式,則用gcc編譯會出現Error: variably modified 'users' at file scope
改成1的形式,則沒有報錯信息。

 

原因:

因為由const定義的是變量,用define定義的宏是常量。C++中可以這么用,但是C中不能這么用。

在c里靜態數組(固定長度的數組,即LISTEN_NUM位置是常數)是允許的,而動態數組(譬如給數組的長度是用變量來表示的)不能寫成UserProfile users[LISTEN_NUM];形式的,只能自行分配內存。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM