问题
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.)
回答1:
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.
回答2:
The array elements of other types must be the same size as the uint16_t
so just cast them. In the case of the 8-bit data, there is a possibility that the upper 8 bits are undefined, so I masked them off.
#include <stdio.h>
#define uint16_t unsigned short
#define int16_t short
#define uint8_t unsigned char
int main() {
uint16_t n;
// convert uint16_t to int16_t
n = 0xFFFF;
printf ("%d\n", (int16_t)n);
// convert uint16_t to uint8_t
n = 168;
printf ("%c\n", (uint8_t)(n & 0xFF));
return 0;
}
Program output is
-1
¿
来源:https://stackoverflow.com/questions/26936057/proper-way-to-access-array-members-as-a-different-type