【发布时间】:2016-10-27 14:15:01
【问题描述】:
我的结构看起来像 -
struct stack{
int top;
char string[size][80];
}stackV;
我想给用户一个在运行时分配 char 字符串数组大小的选项。 我曾使用 scanf 函数来执行此操作 -
我试图通过这样做来实现它-
int size=0;
struct stack{
int top;
char string[size][80];
} stackV;
但是通过这样做,我得到了警告,上面写着 - 在文件范围内可变地修改了“字符串”
有什么方法可以为结构成员数组分配大小。 我无法在任何函数内创建结构,因为该结构成员也被其他函数使用。
【问题讨论】:
-
寻找“灵活数组成员”。
-
如果您使用堆栈数据结构,您应该尝试使用动态内存分配。
-
你可以使用
malloc。 -
@Swanand 我试过这样做 - int size;大小 = (int)malloc(1*sizeof(int));结构栈{int top;字符字符串[大小][80]; }stackV;但我收到警告消息,显示此行有多个标记,其他警告说一元''的无效类型参数(有'int')
-
@SwetaSingh 您应该使用 malloc 将用户输入大小的内存分配给
string。
标签: c arrays data-structures struct structure