Using C I want to allocate heap space of the maximum initial string length which is 6 in my case and then each time the string is about to overflow, double the available space. I want to build the string char by char.
size_t nstring = 6;
char *arr = malloc(nstring);
int i;
for (i = 0; i < 55;i++){
if (i == nstring) {
printf("mAx sized!!");
arr = realloc(arr, nstring*2);
nstring = nstring*2;
}
arr[i] = 'f';
}
arr[i] = '\0';
printf("length %lu\n", strlen(arr));
free(arr);
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire