Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through all the elements of an uint8_t * in iOS

uint8_t *var;
var=//something;

Now I want to loop through each element of this var how to do this please help

like image 941
Deepak Avatar asked Nov 15 '12 07:11

Deepak


1 Answers

Make loop like in plain C. uint8_t *var is just an C array.

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

For example

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);

Note that uint8_t is the same as unsigned char:

#ifndef _UINT8_T
#define _UINT8_T
typedef unsigned char         uint8_t;
#endif /*_UINT8_T */
like image 124
efpies Avatar answered Sep 21 '22 17:09

efpies