struct node
{
int info;
struct node *link;
}*start;
void main()
{
struct node*tmp,*q;
tmp=(struct node*)malloc sizeof(struct node);
}
now my first question is when we declare structure is any struct type node is created in memory?
second question is ,if yes then here i take a start pointer which is pointing to struct type node so without specifying the address of struct node to start pointer how start pointer pointing the struct type node which is created during declaration plz clear me this how internally this is happening i have lot of confusion on that
third question is initially tmp and q pointer variable all are pointing towards the same struct node
plz expalin the concept malloc and calloc how internally they create node
thx for helping me out
You can declare a structure type without declaring any variables. However, your code defines a pointer variable,
start.The variable
startis initialized to 0, so it is not pointing to anything.The variables
tmpandqare not initialized at all and cannot be safely used until assigned a value. You initializetmpin the next line;qis still uninitialized.malloc()provides space for the pointer to point to; it does not initialize that space. The value intmp->infois indeterminate; the value intmp->linkis indeterminate too. If you had usedcalloc(), thentmp->infowould be zero andtmp->linkwould be null (on all practical systems — theoretically, there could be a system wheretmp->linkwas not properly initialized).Finally, note that
void main()is bad. The C standard says that the return type ofmain()should beint. Further, unless you’ve got a C99 or later compiler, you should includereturn(0);or equivalent as the last line inmain()— or a call toexit().