I use realloc() on a string to make its size/memory smaller,so that way i lose the byte that had the null character '\0' in the end.I go and put back the null character for the new smaller size of the string.
The way i know to check a string's size is funtcion strlen(),but strlen will stop when it spots null character.
So the question is did i actually free the space or is strlen just stopping on the null character that i mannualy set at the end.How can i check ?
Just curious,how do you see my post so fast ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * function ()
{
int number;
char *string;
printf("Give number: ");
scanf("%i",&number);
string = (char *) calloc(256,sizeof(char));
printf("Give string: ");
scanf("%s",string);
string = (char *) realloc(string,number+1);//I make the string smaller
string[number] = '\0';//I set the null at the end of the new size
return string;
}
int main()
{
char *string;
string = function();
printf("Size of string is: %i\n",strlen(string));//I check the size of string
puts(string);
return 0;
}