在初始化一个结构数组的值 [英] Initialize values of array in a struct

查看:167
本文介绍了在初始化一个结构数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有它包含几个阵列一个结构。该阵列具有unsigned char型[4]。

I have a struct which has several arrays within it. The arrays have type unsigned char[4].

我可以通过调用初始化每个元素

I can initialize each element by calling

struct->array1[0] = (unsigned char) something;
... 
struct->array1[3] = (unsigned char) something;

只是不知道是否有初始化在一行中的所有4个值的方法。

Just wondering if there is a way to initialize all 4 values in one line.

解决方案:我需要创建一个临时数组的所有值初始化,然后调用memset的()的值复制到阵列结构

SOLUTION: I needed to create a temporary array with all the values initialized, then call memset() to copy the values to the struct array.

推荐答案

如果值是一样的,你可能会做这样的事情。

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;

否则,如果该值存储在一个数组,你可以使用的memcpy 像这样的功能

memcpy(struct->array, some_array, sizeof(struct->array));

这篇关于在初始化一个结构数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆