c - How can i save space in array which is not being used? -
if create array of size 10 , 2 elements stored in array remaining spaces gets wasted. how can solve problem? (by data structure)
you use simple linked list instead of array, or if need use array, should use realloc()
, shrink array use 2 cells, instead of 10. , this:
#include <stdio.h> #include <stdlib.h> int main(void) { int* ptr = malloc(10 * sizeof(int)); ptr[0] = 4; ptr[1] = 13; ptr = realloc(ptr, 2 * sizeof(int)); printf("%d %d\n", ptr[0], ptr[1]); return 0; }
output:
4 13
Comments
Post a Comment