I try to dynamically allocate memory to a string but when I print out the size it shows 4 instead of the (11+1) bytes that should be allocated. Why does this happen? The string prints out just fine.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char c, *string;
int i=0, a=1;
printf("\nEnter the string : ");
string = (char *) malloc (sizeof(char));
while(c != '\n'){
c = getchar();
string = (char *) realloc (string, sizeof(char) * a);
string[i] = c;
a++, i++;
}
string[i] = '\0';
puts(string);
printf("\n%d", sizeof(string));
}
Input : Sample Text
Output :
Sample Text
4