I have a simple program, for learning purposes, yet I keep on getting null returned when I realloc from allocated array.
#include <stdio.h>
#include <stdlib.h>
void main()
{
char *ptr, *retval;
ptr = (char *)calloc(10, sizeof(char));
if (ptr == NULL)
printf("calloc failed\n");
else
printf("calloc successful\n");
retval = (char *)realloc(ptr, 5);
if (retval == NULL)
printf("realloc failed\n");
else
printf("realloc successful\n");
free(ptr);
free(retval);
}
I'm not able to realloc, retval = (char *)realloc(ptr, 5);
what am I doing wrong?