简体   繁体   English

直接初始化或分配变量数组的结构成员,参考命名的结构成员,在 C 中?

[英]Initializing or assigning struct members of variable array directly, with reference to named struct members, in C?

In similar questions on SO and elsewhere, the only solution in this case that is talked about is to initialize the array of structs with array notation, not using dot-names of the struct members.在 SO 和其他地方的类似问题中,在这种情况下讨论的唯一解决方案是使用数组符号初始化结构数组,而不是使用结构成员的点名称。 The closest I have got to what I want is with the following code:我最接近我想要的是使用以下代码:

int texturesCount = 2;
struct textureParams textures[texturesCount];
struct textureParams textures0 =   {
                                        .textureFormat = textureFormat,
                                        .textureAccess = textureAccess,
                                        .srcrect = {.x = 0, .y = 0, .w = 50, .h = 50},
                                        .dstrect = {.x = 0, .y = 500, .w = 50, .h = 50},
                                        .r = 255,
                                        .g = 0,
                                        .b = 0,
                                        .a = 0,
                                    };
struct textureParams textures1 =   {
                                        .textureFormat = textureFormat,
                                        .textureAccess = textureAccess,
                                        .srcrect = {.x = 0, .y = 0, .w = 20, .h = 20},
                                        .dstrect = {.x = 10, .y = 100, .w = 20, .h = 20},
                                        .r = 0,
                                        .g = 255,
                                        .b = 0,
                                        .a = 0,
                                    };
textures[0] = textures0;
textures[1] = textures1;

Is there a way of doing this - creating the struct members of the array with reference to the named struct members - but without creating the temporary variables texture0 and texture1 ?有没有办法做到这一点 - 参考命名的结构成员创建数组的结构成员 - 但不创建临时变量texture0texture1

If you were to for example create an array of int you could initialize it like this:例如,如果您要创建一个int数组,您可以像这样初始化它:

int arr[3] = { 3, 4, 5 };

It's the same in your case except it's an array of structs, and each initializer is a struct initializer:除了它是一个结构数组之外,它与您的情况相同,并且每个初始化程序都是一个结构初始化程序:

struct textureParams textures[texturesCount] = {
    {
        .textureFormat = textureFormat,
        .textureAccess = textureAccess,
        .srcrect = {.x = 0, .y = 0, .w = 50, .h = 50},
        .dstrect = {.x = 0, .y = 500, .w = 50, .h = 50},
        .r = 255,
        .g = 0,
        .b = 0,
        .a = 0,
    },
    {
        .textureFormat = textureFormat,
        .textureAccess = textureAccess,
        .srcrect = {.x = 0, .y = 0, .w = 20, .h = 20},
        .dstrect = {.x = 10, .y = 100, .w = 20, .h = 20},
        .r = 0,
        .g = 255,
        .b = 0,
        .a = 0,
    }
};

Yes, you can directly assign to array's members using compound literals:是的,您可以使用复合文字直接分配给数组的成员:

    int texturesCount = 2;
    struct textureParams textures[texturesCount];

    textures[0] = (struct textureParams) {
            .textureFormat = textureFormat,
            .textureAccess = textureAccess,
            .srcrect = {.x = 0, .y = 0, .w = 50, .h = 50},
            .dstrect = {.x = 0, .y = 500, .w = 50, .h = 50},
            .r = 255,
            .g = 0,
            .b = 0,
            .a = 0,
    };
    textures[1] = (struct textureParams) {
            .textureFormat = textureFormat,
            .textureAccess = textureAccess,
            .srcrect = {.x = 0, .y = 0, .w = 20, .h = 20},
            .dstrect = {.x = 0, .y = 100, .w = 20, .h = 20},
            .r = 0,
            .g = 255,
            .b = 0,
            .a = 0,
    };

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

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