C语言 C 结构体中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5170525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:56:55  来源:igfitidea点击:

Array in C struct

carraysstruct

提问by Greg

I want to have two arrays in a struct, which are initialized at start but need editing further on. I need three instances of the struct, so that I can index into a specific struct and modify as I wish. Is it possible?

我想在一个结构中有两个数组,它们在开始时初始化但需要进一步编辑。我需要该结构的三个实例,以便我可以索引到特定的结构并根据需要进行修改。是否可以?

This is what I thought I could do but I get errors:

这是我认为我可以做但我得到错误:

struct potNumber{
    int array[20] = {[0 ... 19] = 10};
    char *theName[] = {"Half-and-Half", "Almond", "Rasberry", "Vanilla", …};
} aPot[3];

Then I access the structs as follows:

然后我按如下方式访问结构:

 printf("some statement %s", aPot[0].array[0]);
 aPot[0].theName[3];
 …

回答by pmg

The struct themselves do not have data. You need to create objects of the struct type and set the objects ...

结构本身没有数据。您需要创建结构类型的对象并设置对象...

struct potNumber {
    int array[20];
    char *theName[42];
};

/* I like to separate the type definition from the object creation */
struct potNumber aPot[3];
/* with a C99 compiler you can use 'designated initializers' */
struct potNumber bPot = {{[7] = 7, [3] = -12}, {[4] = "four", [6] = "six"}};

for (i = 0; i < 20; i++) {
  aPot[0].array[i] = i;
}
aPot[0].theName[0] = "Half-and-Half";
aPot[0].theName[1] = "Almond";
aPot[0].theName[2] = "Rasberry";
aPot[0].theName[3] = "Vanilla";
/* ... */

for (i = 0; i < 20; i++) {
  aPot[2].array[i] = 42 + i;
}
aPot[2].theName[0] = "Half-and-Half";
aPot[2].theName[1] = "Almond";
aPot[2].theName[2] = "Rasberry";
aPot[2].theName[3] = "Vanilla";
/* ... */

回答by datenwolf

In C struct array elements must have a fixed size, so the char *theNames[]is not valid. Also you can not initialize a struct that way. In C arrays are static, i.e. one cannot change their size dynamically.

在 C 结构体数组元素必须有一个固定的大小,所以char *theNames[]是无效的。您也不能以这种方式初始化结构。在 C 中数组是静态的,即不能动态改变它们的大小。

A correct declaration of the struct would look like the following

结构的正确声明如下所示

struct potNumber{
    int array[20];
    char theName[10][20];
};

and you initialize it like this:

然后像这样初始化它:

struct potNumber aPot[3]=
{
    /* 0 */
    { 
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ },
        {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ }
    },
    /* 1 */
    { 
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ },
        {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ }
    },
    /* 2 */
    { 
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ },
        {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ }
    }
};

But, I'm pretty sure this is not what you want. The sane way to do this required some boilerplate code:

但是,我很确定这不是您想要的。这样做的明智方法需要一些样板代码:

struct IntArray
{
    size_t elements;
    int *data;
};

struct String
{
    size_t length;
    char *data;
};

struct StringArray
{
    size_t elements;
    struct String *data;
};
/* functions for convenient allocation, element access and copying of Arrays and Strings */

struct potNumber
{
    struct IntArray array;
    struct StringArray theNames;
};

Personally I strongly advise against using naked C arrays. Doing everything through helper structs and functions keeps you clear from buffer under/overruns and other trouble. Every serious C coder builds a substancial code library with stuff like this over time.

我个人强烈建议不要使用裸 C 数组。通过辅助结构和函数做所有事情可以让您远离缓冲区不足/溢出和其他问题。随着时间的推移,每个认真的 C 编码员都会用这样的东西构建一个实质性的代码库。