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];形式的,只能自行分配內存。