首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C结构中的数组

C结构中的数组
EN

Stack Overflow用户
提问于 2011-03-03 00:36:44
回答 2查看 105.1K关注 0票数 15

我希望在一个结构中有两个数组,它们在开始时初始化,但需要进一步编辑。我需要这个结构的三个实例,这样我就可以索引到一个特定的结构中,并根据自己的意愿进行修改。有可能吗?

这是我认为我可以做的事情,但我得到了错误:

代码语言:javascript
复制
struct potNumber{
    int array[20] = {[0 ... 19] = 10};
    char *theName[] = {"Half-and-Half", "Almond", "Rasberry", "Vanilla", …};
} aPot[3];

然后,我访问结构,如下所示:

代码语言:javascript
复制
 printf("some statement %s", aPot[0].array[0]);
 aPot[0].theName[3];
 …
EN

回答 2

Stack Overflow用户

发布于 2011-03-03 00:45:58

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

代码语言:javascript
复制
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";
/* ... */
票数 15
EN

Stack Overflow用户

发布于 2011-03-03 00:56:32

在C结构中,数组元素必须具有固定大小,因此char *theNames[]无效。同样,你也不能以这种方式初始化结构。在C中,数组是静态的,也就是说,不能动态地改变它们的大小。

正确的结构声明如下所示

代码语言:javascript
复制
struct potNumber{
    int array[20];
    char theName[10][20];
};

你可以这样初始化它:

代码语言:javascript
复制
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 */ }
    }
};

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

代码语言:javascript
复制
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;
};

就我个人而言,我强烈建议不要使用裸C数组。通过帮助器结构和函数来做所有事情,可以让你避免缓冲区不足/溢出和其他麻烦。随着时间的推移,每一个认真的C程序员都会用这样的东西构建一个实质性的代码库。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5170525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档