0
votes
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

1

1 Answers

2
votes
  1. You can declare a structure type without declaring any variables. However, your code defines a pointer variable, start.

  2. The variable start is initialized to 0, so it is not pointing to anything.

  3. The variables tmp and q are not initialized at all and cannot be safely used until assigned a value. You initialize tmp in the next line; q is still uninitialized.

  4. malloc() provides space for the pointer to point to; it does not initialize that space. The value in tmp->info is indeterminate; the value in tmp->link is indeterminate too. If you had used calloc(), then tmp->info would be zero and tmp->link would be null (on all practical systems — theoretically, there could be a system where tmp->link was not properly initialized).

Finally, note that void main() is bad. The C standard says that the return type of main() should be int. Further, unless you've got a C99 or later compiler, you should include return(0); or equivalent as the last line in main() — or a call to exit().