와 http://codepad.org/wE8dbBV1
가 안전하게 교체 할 수 있습니다. 각 addr
및 id
에 대해 bitfield
을 사용하는 구조체를 사용할 수도 있습니다. 더 편리 할 수도 있지만, 오프셋 정보를 유지하는 쉬운 방법을 제공합니다. addr/id
쌍.
구조체 유형 지정 초기화 자을 uint8_t byte
배열로 채우기 위해 직접 사용할 수있는 방법이 없다고 생각합니다. 가장 가까운 전체 초기화는 memcpy
일 것이라고 생각합니다. 나는 아래 예제에서 그것을 포함시켰다. 참고는 memcpy
으로 uint8_t byte
배열을 작성하지 못하도록 아무것도 없지만, 당신은 정확하게 어떤 주어진 요소 중 하나 addr
또는 id
에 주어진 바이트를 가리 키도록 uint8_t byte
배열 내에서 offset
을 추적 할 수 있습니다. 이것은 비트 필드가 일을 좀 더 쉽게 만들어주는 곳입니다. 당신은 a1..4
및 b1..4
는 각각 각 addr
및 id
, 내 바이트 인으로 struct Device
인덱스와 uibitfield
인덱스 사이 일대일 상관 관계를 얻는다.
uint8_t
배열을 사용하는 버전이이 버전 아래에 표시됩니다. 여기
는
struct Device
의 배열에서 테스트 데이터 짧은 예입니다
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
typedef struct /* bitfield corresponding to struct Device */
{
unsigned int a1 : 8,
a2 : 8,
a3 : 8,
a4 : 8;
unsigned int b1 : 8,
b2 : 8,
b3 : 8,
b4 : 8;
} uibitfield;
struct Device { /* original struct Device */
uint32_t addr;
uint32_t id;
};
int main() {
/* test data in an array of struct Device */
struct Device dev[] = { {0x4009f0, 0}, {0x4009f1, 1}, {0x4009f2, 2}, {0x4009f3, 3},
{0x4009f4, 4}, {0x4009f5, 5}, {0x4009f6, 6}, {0x4009f7, 7},
{0x4009f8, 8}, {0x4009f9, 9}, {0x4009fa, 10}, {0x4009fb, 11},
{0x4009fc, 12}, {0x4009fd, 13}, {0x4009fe, 14}, {0x4009ff, 15},
{0x400a00, 16}, {0x400a01, 17}, {0x400a02, 18}, {0x400a03, 19} };
int it = 0; /* general iterator */
size_t sz = sizeof (dev)/sizeof (*dev); /* size of array */
/* create validate and fill bitfield array */
uibitfield *bytes = calloc (sz, sizeof (*bytes));
if (!bytes) {
fprintf (stderr, "error: allocation failed.\n");
return 1;
}
memcpy (bytes, dev, sz * sizeof (dev));
/* print bytes in each addr & id in dev */
for (it = 0; it < sz; it++)
printf ("\n addr[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n id[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
it, (bytes + it)->a1, (bytes + it)->a2, (bytes + it)->a3, (bytes + it)->a4,
it, (bytes + it)->b1, (bytes + it)->b2, (bytes + it)->b3, (bytes + it)->b4);
printf ("\n");
return 0;
}
출력 :
$ ./bin/memview
addr[ 0]: 0xf0, 0x09, 0x40, 0x00
id[ 0]: 0x00, 0x00, 0x00, 0x00
addr[ 1]: 0xf1, 0x09, 0x40, 0x00
id[ 1]: 0x01, 0x00, 0x00, 0x00
addr[ 2]: 0xf2, 0x09, 0x40, 0x00
id[ 2]: 0x02, 0x00, 0x00, 0x00
addr[ 3]: 0xf3, 0x09, 0x40, 0x00
id[ 3]: 0x03, 0x00, 0x00, 0x00
addr[ 4]: 0xf4, 0x09, 0x40, 0x00
id[ 4]: 0x04, 0x00, 0x00, 0x00
(snip)
참고 :이이었다 불분명 사용이 얼마나/struct Device
을 채우고 stuct Device
에있는 데이터에서 처음 들었던 부분을 얼마만큼 보았는지 알 수 있습니다. 따라서 이는 데이터보기의 예입니다.
당신은`uint8_t 배열을 사용하려는 경우, 필요한 변화가 최소화 :
/* using a uint8_t byte array */
uint8_t *bytearr = calloc (sz * 4, sizeof (*bytearr));
if (!bytearr) {
fprintf (stderr, "error: allocation failed.\n");
return 1;
}
memcpy (bytearr, dev, sz * sizeof (dev));
/* print bytes in each addr & id in dev using uint8_t array */
for (it = 0; it < sz * 4; it+=8)
printf ("\n addr[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n id[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
it, bytearr[it], bytearr[it+1], bytearr[it+2], bytearr[it+3],
it, bytearr[it+4], bytearr[it+5], bytearr[it+6], bytearr[it+7]);
출력이 같은
하는 uint8_t 바이트 배열을 사용하여
첫 번째. 왜 그걸하고 싶니? –
그래서 구조체 값을 배열에 넣기 만하면됩니까? – Nit
이유가있는 게시물을 업데이트했습니다. 나는 더 큰 메모리 덩어리를 예약하지만 단순한 방법으로 구조체에 사용 된 부분을 초기화하고자하는 임베디드 시스템을 사용하고 있습니다. –