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 0xff mark end of valid data in array

    example:

    #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 char binary data. char may aliasing signed char, might run problems because signed char contains sign bit.

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -