Consider the below program
Code:
struct str
{
char *p;
int i;
}t;
main()
{
scanf("%s %d",t.p,&t.i);
printf("%s",t.p);
}
the output is : Runtimer Error
The reason is given that as we are trying to store the string at the address to which str(of t) points to. Since the pointer is not initialized it holds garbage value and so the reslult will be undefined. Hence the error.
Then what about this
Code:
void main()
{
char *p;
scanf("%s",p);
printf("%s",p);
}
Here, though the pointer variable p is not initialized, it is accepting the input from the user and printing it without showing any error..
May I know the reason in detail??