简体   繁体   English

如何在运行时给c中的结构数组成员的大小

[英]How to give size of a structure array member in c at runtime

I have a structure which looks like - 我的结构看起来像 -

struct stack{
 int top;
 char string[size][80];
}stackV;

I want to give user an option to allocate size of char string array at runtime. 我想给用户一个选项来在运行时分配char字符串数组的大小。 I had used scanf function to do this - 我用过scanf函数来做到这一点 -

I tried to achive it by doing - 我试图通过以下方式实现 -

int size=0;
 struct stack{
  int top;
  char string[size][80];
 } stackV;

But by doing this i got warning which says - variably modified 'string' at file scope 但通过这样做我得到警告说 - 在文件范围可变地修改'字符串'

Is there any way by which we can assign size to a structure member array . 我们可以通过哪种方式为结构成员数组指定大小。 I can't create a structure inside any function because the structure member is used by other functions also. 我无法在任何函数内部创建结构,因为结构成员也被其他函数使用。

You can use a flexible array member to achieve what you need: 您可以使用灵活的阵列成员来实现您的需求:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct 
{
   int top;
   char string[][80];
} stackv;

int main() 
{
  size_t strings = 3;

  stackv* s = malloc(sizeof(stackv) + strings*80);
  strcpy(s->string[0], "test");
  strcpy(s->string[1], "hello");
  strcpy(s->string[2], "world");

  for(size_t i=0; i<strings; i++)
  {
    puts(s->string[i]);
  }

  free(s);
  return 0;
}

The declaration of char string[][80]; char string[][80];的声明char string[][80]; tells the compiler that there will be an unknown amount of char [80] arrays at the end of the struct. 告诉编译器结构末尾会有未知数量的char [80]数组。

The sizeof(stackv) will therefore only give the size of all other members except the last one (in this case an integer). 因此, sizeof(stackv)将仅给出除最后一个之外的所有其他成员的大小(在本例中为整数)。

You don't know how many elements will be in your stack. 您不知道堆栈中有多少元素。 Only the user gives you the "size" in the runtime; 只有用户在运行时给出“大小”; so you can't allocate an array statically (compilation time), but allocate it dynamically (run time) when getting the size from the user. 因此,您无法静态分配数组(编译时间),但在从用户获取大小时动态分配它(运行时)。 You are getting an "warning" because you define "size" from size "0" then you try to declare an array of this size and according to the spec there is no array of size 0 as I understand): 你得到一个“警告”,因为你从大小“0”定义“大小”,然后你尝试声明这个大小的数组,根据规范,没有像我所理解的大小0的数组):

ISO 9899:2011 6.7.6.2:

    If the expression is a constant expression, it shall have a value greater than zero.

How to allocate the array dynamically as a struct which contains "size" elements and each element is a string of 80 chars? 如何动态地分配数组作为包含“size”元素的结构,每个元素是一个包含80个字符的字符串?

#define STR_LEN 80
struct stack {
   int top;
   char *string;
} stackv;

enum {
    SUCCESS = 0,
    FAILURE,
};

int InitStack(stackv *stackv, int size) {
    if (size <= 0)
        return FAILURE;

    stackv->string = (char *)malloc(size*sizeof(char)*80);
    if (stackv->string == NULL)
        return FAILURE;

    stackv->top = 0;

    return SUCCESS;
}

In the main function, you declare the variable "stackv" and pass it by reference (pointer) to the initialization function which I wrote. 在main函数中,声明变量“stackv”并通过引用(指针)将其传递给我编写的初始化函数。

This code compiles correctly for me: 这段代码正确编译给我:

int size = 10;

typedef struct 
{
   int top;
   char string[size][80];
} stackv;

I'm using: 我正在使用:

$ gcc --version
gcc (Debian 5.4.1-3) 5.4.1 20161019
Copyright (C) 2015 Free Software Foundation, Inc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM