I am new to C language, and the tutorial is here.
I follow the tutorial, but it will not give an error when i try to not realloc memory,
the results are same whether comments the realloc code or not.
I want to know why? Can someone explain this? Thanks
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[100];
char *description;
strcpy(name, "Zara Ali");
description = (char *) malloc(30 * sizeof(char));
if (description == NULL)
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcpy(description, "Zara li a DPS student.");
}
// description = (char *) realloc(description, 100 * sizeof(char));
if (description == NULL)
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcat(description, "She is in class 10th.");
}
printf("Name = %s\n", name);
printf("Description: %s\n", description);
free(description);
}
result: picture