realloc : realloc « stdlib.h « C Tutorial






ItemValue
Header filestdlib.h
Declarationvoid *realloc(void *ptr, size_t size);
Functionreallocate memory
ParameterIf ptr is null, realloc() simply allocates size bytes of memory and returns a pointer. If size is zero, the memory pointed to by ptr is freed.


#include <stdlib.h>
  #include <stdio.h>
  #include <string.h>
  int main(void)
  {
    char *p;

    p = malloc(17);
    if(!p) {
      printf("Allocation Error\n");
      exit(1);
    }

    strcpy(p, "This is 16 chars");

    p = realloc(p, 18);
    if(!p) {
      printf("Allocation Error\n");
      exit(1);
    }

    strcat(p, ".");

    printf(p);

    free(p);

    return 0;
  }
This is 16 chars.








23.25.realloc
23.25.1.realloc