简体   繁体   English

将数组分配给结构中的数组

[英]Assign an array to array in struct

I'm trying to assign an array to one of the fields of a typedef struct and I can't find a way to do it practically.我正在尝试将数组分配给 typedef 结构的一个字段,但我找不到实际的方法。

I've searched for this problem but all I seem to find is answers for char * arrays which is not what I'm looking for, I'm just trying to assign an array to an int array, and looking for a practical way for the code below to work without having to initialize all the variables in the struct (they will be initialized later, but I just want to set the array variable):我已经搜索了这个问题,但我似乎找到的只是 char * 数组的答案,这不是我要找的,我只是想将一个数组分配给一个 int 数组,并寻找一种实用的方法下面的代码无需初始化结构中的所有变量即可工作(稍后将对其进行初始化,但我只想设置数组变量):

typedef struct {
    int array[5];
    int number;
} Rot;

Rot RA;

void config()
{
    RA.array = {1, 2, 3, 4, 5}; //This returns an "expected expression before "{"" error
    int arr[5];
    int i;
    for (i = 0; i < 5; i++)
    {
        arr[i] = i + 1;
    }
    RA.array = arr; //I understand why this fails, but I need to do this in a practical way
}

Please assume that config is called later and the struct and RA are all accessible to it.请假设稍后调用 config 并且结构和 RA 都可以访问它。

RA.array = {1, 2, 3, 4, 5};

memcpy(RA.array, (int[]){1, 2, 3, 4, 5}, sizeof RA.array);

RA.array = arr;

memcpy(RA.array, arr, sizeof arr); // better: sizeof RA.array

You can use memcpy as shown in another answer.您可以使用memcpy ,如另一个答案所示。 Or alternatively, copy the whole struct and not just the array, using a temporary variable in the form of a compound literal :或者,使用复合文字形式的临时变量复制整个结构而不仅仅是数组:

RA = (Rot) { {1, 2, 3, 4, 5}, 0 };

This is possible because while C doesn't allow run-time assignment of arrays, it does allow it of structs.这是可能的,因为虽然 C 不允许数组的运行时分配,但它允许结构体。

You can use memcpy as shown in another answer, or copy the whole struct as shown in another answer (although your question states that you just want to set the array, not the remainder of the struct).您可以使用另一个答案中所示的memcpy ,或复制另一个答案中所示的整个结构(尽管您的问题指出您只想设置数组,而不是结构的其余部分)。

Another option is to embed just the array into another struct:另一种选择是将数组嵌入到另一个结构中:

typedef struct {
    int elem[5];
} RotArr;

typedef struct {
    RotArr arr;
    int number;
} Rot;

Then you can access element i of the array in Rot RA as RA.arr.elem[i] .然后您可以访问Rot RA数组的元素i作为RA.arr.elem[i] Also, you can assign values to a whole RotArr object.此外,您可以为整个RotArr对象赋值。 The remainder of your code could look something like this:您的代码的其余部分可能如下所示:

Rot RA;

void config(void)
{
    RA.arr = (RotArr){{1, 2, 3, 4, 5}};
    RotArr arr;
    int i;
    for (i = 0; i < 5; i++)
    {
        arr.elem[i] = i + 1;
    }
    RA.arr = arr;
}

Note that (RotArr){{1, 2, 3, 4, 5}} is a compound literal value of RotArr type.请注意, (RotArr){{1, 2, 3, 4, 5}}RotArr类型的复合文字值。 It could also be written as (RotArr){ .elem = {1, 2, 3, 4, 5} } or (RotArr){ .elem = { [0] = 1, [1] = 2, [2] = 3, [3] = 4, [4] = 5 } } to be absolutely clear which parts of the compound literal are being set explicitly (any remaining parts will be set to 0), but since it only has a single member, these forms of the compound literal value are a bit over-the-top.它也可以写成(RotArr){ .elem = {1, 2, 3, 4, 5} }(RotArr){ .elem = { [0] = 1, [1] = 2, [2] = 3, [3] = 4, [4] = 5 } }绝对清楚复合文字的哪些部分被显式设置(任何剩余部分将被设置为 0),但由于它只有一个成员,这些复合文字值的形式有点过头了。

The following works according to C syntax.以下根据 C 语法工作。 Not sure this is what you wanted.不确定这是你想要的。

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

typedef struct {
    int array[5];
    int number;
} Rot;

Rot RA = {{1,2,3,4,5}};

void main()
{
    RA = (Rot) {{5, 6, 7, 8, 9}};
    int arr[5];
    int i;
    for (i = 0; i < 5; i++)
    {
        arr[i] = i + 1;
    }

    memmove(RA.array, arr, sizeof(RA.array));
    // OR
    
    int l = sizeof(arr)/sizeof(arr[0]);
    for(int i =0 ; i < l ; ++i) {
        *(RA.array + i) = *(arr + i);
        printf("%d\n",RA.array[i]);
    }
}

Moreover, use memmove since that allows memory overlap.此外,请使用 memmove,因为它允许内存重叠。

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

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