是否可以像下面的struct IP_addresses ip_addr[no_of_users]
那样初始化一个可变大小的对象?
void foo(int no_of_users){
struct IP_addresses{
char IPaddr[16];
};
struct IP_addresses ip_addr[no_of_users];
//.....
}
int main(void){
int no_of_users = 4;
foo(no_of_users);
return 0;
}
最佳答案
您不能初始化变长数组。来自 C 2011 (N1570) 6.7.9,“初始化”,第 3 段:
The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.
您必须通过另一种方法为数组赋值,例如循环赋值每个元素。
关于c - 如何初始化可变大小的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128690/