swapping between members of a string array in C -
i want remove i-th member of string array, , bring every member comes after 1 place prior (the i+1-th member , on). came following code:
(int j = i; j < arrsize - 1; j++) { strcpy(members[j],members[j+1]); } free(members[arrsize-1]);
but got me thinking might wrong. example, if i-th place member contains name "john", while (i+1)-th place members contains name "joshua", means each string in different length, there memory leaks or problem? in advance!
edit: definiton of members:
members = malloc(maxmembersnum * sizeof(char*));
rather copy contents of strings, why not move pointers around? is:
for (int j = i; j < arrsize - 1; j++) { char *temp = members[j]; members[j] = members[j+1]; members[j+1] = temp; } free(members[arrsize-1]);
Comments
Post a Comment