Remove some elements from array and re-size array in C -
regards
want remove elements array , re-size it.
example array is:
char get_res[6] = {0x32,0x32,0x34,0x16,0x00,0x00}; now want remove elements after 0x16, desire array is:
get_res[] = {0x32,0x32,0x34,0x16};
what solution?
you cannot resize arrays in c (unlike python, example). real resizing, @ least api user's point of view, use malloc, calloc, realloc, , free (realloc specifically).
anyway, "resizing" array can imitated using
a delimiter; example, delimiter
0xffmark end of valid data in arrayexample:
#define delimiter 0xff print_data(char* data) { (size_t = 0; data[i] != delimiter; ++i) printf("%x", data[i]); }a member counter; count number of valid data beginning of array onward
example:
size_t counter = 5; print_data(char* data) { (size_t = 0; < counter; ++i) printf("%x", data[i]); }
notes:
- use
unsigned charbinary data.charmay aliasingsigned char, might run problems becausesigned charcontains sign bit.
Comments
Post a Comment