繁体   English   中英

在c中分配给struct中的数组

[英]Assign to array in struct in c

我有以下代码:

typedef struct Test {
  long mem[1000];
} Test;

extern Test *test;
int main() {
    Test *test =  (Test *)malloc(sizeof(Test)); 
    test->mem[0] = 1;
    test->mem[1] = 2;
    test->mem[2] = 3;
    test->mem[3] = 4;
    test->mem[4] = 5;
    test->mem[5] = 6;
   return 0;
}

它工作正常,但我想以这种方式改变mem数组的初始化:

test->mem = {1,2,3,4,5,6};

但是gcc给了我这个错误:

错误:'{'token test-> mem = {1,2,3,4,5,6}之前的预期表达式; 用箭头指向左侧开括号。

它可以是什么?

谢谢!

编辑:我也尝试这个代码:

long mem[1000] = {1,2,3,4,5,6};
    test->mem = mem;

我从gcc得到这个错误:

错误:从类型'long int *'test-> mem = mem中分配类型'long int [1048576]'时出现不兼容的类型;

我不允许使用任何C函数。

语法something = { initial values }仅在初始化时允许,其中定义了一个对象,例如:

long mem[1000] = { 1, 2, 3, 4, 5, 6 };

诸如x = value类的表达式是赋值 ,不能使用语法进行初始化。

另一种方法是创建一个初始化的临时对象,然后将该临时对象的内容复制到目标中:

static const long temporary[] = { 1, 2, 3, 4, 5, 6 };
memcpy(test->mem, temporary, sizeof temporary);

关于编辑:

可能不会分配数组; 如果x是数组,则x = value无效。 但是,可以分配结构,因此另一种方法是将结构创建为临时对象,对其进行初始化并分配:

// (After the malloc is successful.)
static const Test temporary = { { 1, 2, 3, 4, 5, 6 } };
*test = temporary;

但是请注意,此代码执行的先前代码没有。 我前面的例子只展示了六个元素到数组中。 此代码创建一个Test类型的临时对象,其中包含1000个元素,其中大多数为零,并将所有这些元素复制到*test 即使编译器对此进行优化并使用某些代码来清除*test而不是实际复制存储在内存中的零,也需要比复制六个元素更长的时间。 因此,如果您只想初始化一些元素并且不关心其余元素,请使用以前的代码。 如果要将所有元素初始化(大多数为零),则可以使用后一个代码。 (即便如此,我会考虑替代方案,比如使用calloc而不是malloc 。)

数组不是指针(但数组会衰减到指针,请参阅此内容 ),并且您无法分配数组 (仅初始化它们,或指定包含它们的struct -s)。 你可以复制数组,例如

Test *test =  (Test *)malloc(sizeof(Test)); 
if (!test) { perror("malloc"); exit(EXIT_FAILURE); };
static const int arr[] = {1,2,3,4,5,6};
memcpy (test->mem, arr, sizeof(arr));

顺便说一句,您可以通过编写循环for (int i=0; i<6; i++) test->mem[i] = arr[i];复制而不使用memcpy for (int i=0; i<6; i++) test->mem[i] = arr[i]; ....

这使得9994个整数在test未初始化; 你可能想要清除它们:

memset (test->mem+6, 0, 9994*sizeof(int));

或使用另一个for循环。

您还可以定义初始化结构,例如

Test mystruct = {0, 2, 4, 6, 8};

然后分配它,例如

*test = mystruct;

但你不能分配数组! 甚至

// wrong code, won't compile
int ta[4] = { 0, 1, 2, 3}; // initialization, not assignment
int tb[4] = { 2, 4, 6, 8}; // ditto
int *tp = &ta;
ta = tb; // wrong!
tb = tp; // also wrong

不会编译。

FWIW, C ++ 11std :: array来帮助解决这个问题。

C11标准的§6.5.16.1简单赋值部分(参见n1570草案第102页)列出了一组关于赋值的约束,并且数组赋值不适合那里。 因此被禁止。 根据经验,只有标量(包括指针和数字l值 )或struct可以出现在赋值的左侧(或者从函数return )。

暂无
暂无

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

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