I have a struct which has several arrays within it. The arrays have type unsigned char[4].
我有一个结构体里面有几个数组。数组的类型为无符号字符[4]。
I can initialize each element by calling
我可以通过调用来初始化每个元素
struct->array1[0] = (unsigned char) something;
...
struct->array1[3] = (unsigned char) something;
Just wondering if there is a way to initialize all 4 values in one line.
我想知道是否有一种方法可以在一行中初始化所有的4个值。
SOLUTION: I needed to create a temporary array with all the values initialized, then call memset() to copy the values to the struct array.
解决方案:我需要创建一个初始化了所有值的临时数组,然后调用memset()将值复制到struct数组。
6 个解决方案
#1
5
If the values are the same, you might do something like
如果值是相同的,您可以做类似的事情
struct->array[0] = struct->array[1] = struct->array[2] = struct->array[3] = (unsigned char) something;
Otherwise, if the values are stored in an array, you can use the memcpy function like so
否则,如果值存储在数组中,可以像这样使用memcpy函数
memcpy(struct->array, some_array, sizeof(struct->array));
#2
23
If you really mean "initialize" in the sense that you can do it at the time you declare the variable, then sure:
如果你真正的意思是“初始化”,你可以在声明变量的时候进行,那么请确保:
struct x {
unsigned char array1[4];
unsigned char array2[4];
};
struct x mystruct = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 }
};
#3
10
When you create the struct, you can initialise it with aggregate initialisation:
当您创建结构体时,您可以使用聚合初始化来初始化它:
struct test {
int blah;
char arr[4];
};
struct test = { 5, { 'a', 'b', 'c', 'd' } };
#4
2
Yes:
是的:
struct Foo
{
unsigned char a[4];
unsigned char b[4];
};
struct Foo x = { { 1, 2, 3, 'a' }, { 'a', 'b', 'c', 0 } };
#5
2
I see you have a pointer (do you?).
我看到你有一个指针(是吗?)
If you allocate memory for the pointer with calloc()
everything inside the struct will be initialized with 0
.
如果使用calloc()为指针分配内存,则结构体中的所有内容都将初始化为0。
Otherwise you need to memset()
to 0 or assign a value element-by-element.
否则,需要将memset()赋值为0或按元素分配值。
memset(struct_pointer, 0, sizeof *struct_pointer);
#6
0
You can loop too:
你也可以循环:
for(i = 0; i < 4; i++) the_struct->array1[i] = (unsigned char) something;
This will work even when you have not char but e.g. int (and values != 0). In fact, memsetting to, say, 1 a struct made of int (when sizeof int greater than 1) is not the correct way to initialize them.
即使没有char,它也可以工作,例如int(和值!= 0)。
#1
5
If the values are the same, you might do something like
如果值是相同的,您可以做类似的事情
struct->array[0] = struct->array[1] = struct->array[2] = struct->array[3] = (unsigned char) something;
Otherwise, if the values are stored in an array, you can use the memcpy function like so
否则,如果值存储在数组中,可以像这样使用memcpy函数
memcpy(struct->array, some_array, sizeof(struct->array));
#2
23
If you really mean "initialize" in the sense that you can do it at the time you declare the variable, then sure:
如果你真正的意思是“初始化”,你可以在声明变量的时候进行,那么请确保:
struct x {
unsigned char array1[4];
unsigned char array2[4];
};
struct x mystruct = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 }
};
#3
10
When you create the struct, you can initialise it with aggregate initialisation:
当您创建结构体时,您可以使用聚合初始化来初始化它:
struct test {
int blah;
char arr[4];
};
struct test = { 5, { 'a', 'b', 'c', 'd' } };
#4
2
Yes:
是的:
struct Foo
{
unsigned char a[4];
unsigned char b[4];
};
struct Foo x = { { 1, 2, 3, 'a' }, { 'a', 'b', 'c', 0 } };
#5
2
I see you have a pointer (do you?).
我看到你有一个指针(是吗?)
If you allocate memory for the pointer with calloc()
everything inside the struct will be initialized with 0
.
如果使用calloc()为指针分配内存,则结构体中的所有内容都将初始化为0。
Otherwise you need to memset()
to 0 or assign a value element-by-element.
否则,需要将memset()赋值为0或按元素分配值。
memset(struct_pointer, 0, sizeof *struct_pointer);
#6
0
You can loop too:
你也可以循环:
for(i = 0; i < 4; i++) the_struct->array1[i] = (unsigned char) something;
This will work even when you have not char but e.g. int (and values != 0). In fact, memsetting to, say, 1 a struct made of int (when sizeof int greater than 1) is not the correct way to initialize them.
即使没有char,它也可以工作,例如int(和值!= 0)。