简体   繁体   English

在C中初始化结构数组

[英]Initializing Array of Structure in C

I have the following structure 我有以下结构

typedef struct {
    int buf[BUF_SIZE]; // the buffer
    size_t len; // number of items in the buffer
    pthread_mutex_t mutex; // needed to add/remove data from the buffer
    pthread_cond_t can_produce; // signaled when items are removed
    pthread_cond_t can_consume; // signaled when items are added
};

Initially I was simply initializing it as follows 最初我只是按如下方式对其进行初始化

buffer_t buffer = {
    .len = 0,
    .mutex = PTHREAD_MUTEX_INITIALIZER,
    .can_produce = PTHREAD_COND_INITIALIZER,
    .can_consume = PTHREAD_COND_INITIALIZER
};

Although I would like to initialize an array of buffer_t with those values, although I'm not quite sure how to properly do it. 尽管我想使用这些值初始化buffer_t数组,但是我不确定如何正确地执行该操作。

Something like 就像是

buffer_t buffer[NUM_ARRAY] = {
    .len = 0,
    .mutex = PTHREAD_MUTEX_INITIALIZER,
    .can_produce = PTHREAD_COND_INITIALIZER,
    .can_consume = PTHREAD_COND_INITIALIZER
};

(Which I realize is incorrect) (我意识到这是不正确的)

Edit: I ended up using 编辑:我最终使用

buffer_t buffers[NUM_THREADS];

for (i = 0, i < 3, i ++) {
       buffers[i] = (buffer_t) {
                .len = 0,
                .mutex = PTHREAD_MUTEX_INITIALIZER,
                .can_produce = PTHREAD_COND_INITIALIZER,
                .can_consume = PTHREAD_COND_INITIALIZER
            };  
}

If NUM_ARRAY isn't too big, you can do something like this: 如果NUM_ARRAY不太大,则可以执行以下操作:

#define NUM_ARRAY 3

buffer_t buffer[NUM_ARRAY] = {
    { {0}, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER},
    { {0}, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER},
    { {0}, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER}
};

Or you could explicitly code it like this: 或者您可以像这样显式地编写代码:

buffer_t buffer[NUM_ARRAY];
int i;
for (i=0; i<NUM_ARRAY; i++) {
    memset(buffer[i].buf, 0, sizeof(buffer[i].buf));
    buffer[i].len = 0;
    buffer[i].mutex = PTHREAD_MUTEX_INITIALIZER;
    buffer[i].can_produce = PTHREAD_COND_INITIALIZER;
    buffer[i].can_consume = PTHREAD_COND_INITIALIZER;
}

EDIT: 编辑:

So it looks like the PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER macros can't be used in an assignment like this as it's meant to be used only in initializations and contains { and } characters. 因此,看起来PTHREAD_MUTEX_INITIALIZERPTHREAD_COND_INITIALIZER宏无法在这样的分配中使用,因为它仅用于初始化,并且包含{}字符。

So you need to use initialization syntax as others have suggested: 因此,您需要像其他人建议的那样使用初始化语法:

buffer_t buffer[NUM_ARRAY];
int i;
for (i=0; i<NUM_ARRAY; i++) {
    buffer[i] = (buffer_t) {
        0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER
    };
}

You need to specify the curly braces to initialize the array, so your last example is incorrect indeed. 您需要指定花括号来初始化数组,因此最后一个示例确实是不正确的。

There are multiple ways to initialize the whole array with the same struct every time: 每次有多种方法可以使用相同的struct初始化整个数组:

  1. Initialize each array member explicitly. 显式初始化每个数组成员。 Example: 例:

     buffer_t buffer[NUM_ARRAY] = { { .len = 0, .mutex = PTHREAD_MUTEX_INITIALIZER, ... }, { .len = 0, .mutex = PTHREAD_MUTEX_INITIALIZER, }, ... }; 

    This is pretty tedious, though. 但是,这非常繁琐。 A standardese way would be to... 一种标准的方式是...

  2. Use a for loop: 使用for循环:

     buffer_t buffer[NUM_ARRAY]; for (size_t i = 0; i < NUM_ARRAY; ++i) { buffer[i] = (buffer_t) { .len = 0, .mutex = PTHREAD_MUTEX_INITIALIZER, ... }; } 

    Way better 1 . 更好的方式 1。 However, there's still... 但是,仍然...

  3. Designated initializer lists . 指定的初始化程序列表 Some of them have been standardized like ones you are using but this one hasn't. 其中一些已经标准化,就像您正在使用的那样,但是还没有。 Therefore it's a non-standard GCC extension but worth to mention anyway. 因此,这是一个非标准的GCC扩展,但无论如何值得一提。 Example: 例:

     buffer_t buffer[NUM_ARRAY] = { [0 ... (NUM_ARRAY - 1)] = { .len = 0, .mutex = PTHREAD_MUTEX_INITIALIZER, ... } }; 

I recommend the 2 nd option, though. 我推荐第二个选项。


1 The (buffer_t) { } syntax is available only since C99. 1 (buffer_t) { }语法仅自C99起可用。 If your compiler doesn't support it, use simple assignment per struct member. 如果您的编译器不支持,请为每个struct成员使用简单分配。

If your buffer is global, you can leave the initialization in this special case, because PTHREAD_MUTEX_INITIALIZER is a structure with all zeroes. 如果缓冲区是全局缓冲区,则可以在这种特殊情况下保留初始化,因为PTHREAD_MUTEX_INITIALIZER是全为零的结构。 Otherwise, you would have no other choice than to initialize all members or loop over it programmatically. 否则,除了初始化所有成员或以编程方式对其进行循环外,您别无选择。

Generally you can initialise an array of structs something like this: 通常,您可以初始化类似以下结构的数组:

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

typedef struct buffer_t {
    int a;
    int b;
    int c;
} buffer_t;

int main()
{
    int i;
    int NUM_ARRAY=4;

    buffer_t buffer[NUM_ARRAY];

    for(i=0; i<NUM_ARRAY; i++)
    {
        buffer[i].a = 0;
        buffer[i].b = 1;
        buffer[i].c = 2;
    };

    return 0;
}

I feel like you're after more than this though? 我觉得您比这还追赶吗?

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

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