正确的方式来访问数组成员为不同类型的? [英] Proper way to access array members as a different type?
问题描述
我有一个大阵的uint16_t
。
它的大部分成员是 uint16_t
,但有些是 int16_t
有的 uint8_t有
。
你会如何处理这个问题?
顺便说一句,我想:
-
指针:
用于2球,有一个
int16_t *
和其他uint8_t有*
,既初始化数组的开始,访问数组成员是int16_t
和uint8_t有
。(即最初的工作,但我遇到了问题时,后面的程序别的东西改变了指针的值,所以我不相信它。)
-
类型定义为联合。
在file.h:
的typedef工会{
uint16_t U16 [NO_OF_WORDS] //由于uint16_t
int16_t S16 [NO_OF_WORDS] //由于int16_t
uint8_t有U8 [2 * NO_OF_WORDS]。 //由于uint8_t有
} ram_params_t;
EXTERN ram_params_t ram_params [];在file.c中:
ram_params_t ram_params [] = {0};
(这确实轰炸了。)
-
铸造。
(我没有走得很远这一点。)
你尝试#2的问题是,你做一个数组的数组。
做这一点:
的typedef工会{
uint16_t U16; //由于uint16_t
int16_t S16; //由于int16_t
uint8_t有U8 [2]; //由于uint8_t有
} ram_params_t;
EXTERN ram_params_t ram_params [NO_OF_WORDS]ram_params_t ram_params [NO_OF_WORDS]uval16 = ram_params [I] .u16;
sval16 = ram_params [I] .s16;
uval8_1 = ram_params [I] .u8 [0];
uval8_2 = ram_params [Ⅰ] .u8 [1];
或者你做到这一点:
的typedef工会{
uint16_t U16 [NO_OF_WORDS] //由于uint16_t
int16_t S16 [NO_OF_WORDS] //由于int16_t
uint8_t有U8 [2 * NO_OF_WORDS]。 //由于uint8_t有
} ram_params_t;
EXTERN ram_params_t ram_params;ram_params_t ram_params;uval16 = ram_params.u16 [I]
sval16 = ram_params.s16 [I]
uval8_1 = ram_params.u8 [2 * i];
uval8_2 = ram_params.u8 [2 * I + 1];
我看不出什么毛病你尝试#1无论是。我想,我可能会做,而不是使用一个联盟。
I have a large array of uint16_t
.
Most of its members are uint16_t
, but some are int16_t
and some uint8_t
.
How would you handle that?
By the way, I tried:
Pointers:
Used 2 pointers, one
int16_t*
and the otheruint8_t*
, both initialized to the start of the array, to access member of the array that areint16_t
anduint8_t
.(That worked initially, but I ran into problems when later in the program something else changed the value of the pointers, so I don't trust it.)
Type definition with a union.
In file.h:
typedef union { uint16_t u16[NO_OF_WORDS]; // As uint16_t int16_t s16[NO_OF_WORDS]; // As int16_t uint8_t u8[2 * NO_OF_WORDS]; // As uint8_t } ram_params_t; extern ram_params_t ram_params[];
In file.c:
ram_params_t ram_params[] = {0};
(That really bombed.)
Casting.
(I didn't get very far with that.)
The problem with your attempt #2 was that you made an array of arrays.
Either do this:
typedef union {
uint16_t u16; // As uint16_t
int16_t s16; // As int16_t
uint8_t u8[2]; // As uint8_t
} ram_params_t;
extern ram_params_t ram_params[NO_OF_WORDS];
ram_params_t ram_params[NO_OF_WORDS];
uval16 = ram_params[i].u16;
sval16 = ram_params[i].s16;
uval8_1 = ram_params[i].u8[0];
uval8_2 = ram_params[i].u8[1];
Or you do this:
typedef union {
uint16_t u16[NO_OF_WORDS]; // As uint16_t
int16_t s16[NO_OF_WORDS]; // As int16_t
uint8_t u8[2 * NO_OF_WORDS]; // As uint8_t
} ram_params_t;
extern ram_params_t ram_params;
ram_params_t ram_params;
uval16 = ram_params.u16[i];
sval16 = ram_params.s16[i];
uval8_1 = ram_params.u8[2*i];
uval8_2 = ram_params.u8[2*i+1];
I don't see anything wrong with your attempt #1 either. I think that I would probably do that rather than using a union.
这篇关于正确的方式来访问数组成员为不同类型的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!