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