在iOS中循环遍历uint8_t *的所有元素

时间:2012-11-15 07:06:14

标签: objective-c ios

uint8_t *var;
var=//something;

现在我想遍历此var的每个元素 这该怎么做 请帮忙

1 个答案:

答案 0 :(得分:4)

像普通C一样制作循环。uint8_t *var只是一个C数组。

for (uint8_t i = 0; i < ARRAY_LENGTH; ++i) {
    var[i] = ...; // Do whatever you want
}

例如

uint8_t *v = (uint8_t *)malloc(5 * sizeof(uint8_t));

for (uint8_t i = 0; i < 5; ++i) {
    v[i] = i;
}

for (uint8_t i = 0; i < 5; ++i) {
    NSLog(@"%d\n", v[i]);
}

free(v);

请注意,uint8_tunsigned char相同:

#ifndef _UINT8_T
#define _UINT8_T
typedef unsigned char         uint8_t;
#endif /*_UINT8_T */