c - Can I realloc from a position of the array? -
say declared array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
. later want array[8] = {2, 3, 4, 5, 6, 7, 8, 9}
.
dismiss first 2 elements. start on array[2]
. reallocing array
array[2]
.
i tried:
int *array=(int*)malloc(10*sizeof(int)); ...//do stuffs array=(int*)realloc(array[2],8*sizeof(int));
it didn't work. neither using &array[2], *array[2]
, nor creating auxiliary array, reallocing array auxarr free(auxarr).
can light?
you can realloc pointer memory block has been alloc'ed. can realloc(array), not array[2] since pointer location in middle of memory block.
you may want try memmove instead.
edit:in response thingywotsit's comment, after memomving data want front of array, can realloc drop off tail end.
Comments
Post a Comment