-1

code

#include <stdio.h>

void main()
{
     struct Node      //defining a node
     {
          int data;             //defining the data member
          struct Node *next;    //defining a pointer to Node type variable
     };

      struct Node *head;        //declaring a pointer variable 'head' to a Node type variable.   
      head=NULL;                //Since the head pointer now points nothing,so initialised with NULL.

      struct Node* temp = (Node*)malloc(sizeof(struct Node));//creating a node and storing its adrs in pointer 'temp'
     (*temp).data=2;          //node's data part is initialised.
     (*temp).next= NULL;    //the node points to nothing initially
     head=temp;             //head is initialised with address of the node,so now it points to the node 

     printf("%d",temp->data);
 }
amdixon
  • 3,814
  • 8
  • 25
  • 34
ABHISHEK KUMAR
  • 71
  • 1
  • 10
  • 1
    Do not cast the return of `malloc()`. I think i should keep a template of this comment. :p – Haris Sep 13 '15 at 09:05
  • post the error message – amdixon Sep 13 '15 at 09:06
  • As I am naive in it,would you please elaborate Sir? LinkedListImp.c:14:25: error: ‘Node’ undeclared (first use in this function) struct Node* temp = (Node*)malloc(sizeof(struct Node));//creating a node and storing its adrs in pointer 'temp' ^ LinkedListImp.c:14:25: note: each undeclared identifier is reported only once for each function it appears in LinkedListImp.c:14:30: error: expected expression before ‘)’ token struct Node* temp = (Node*)malloc(sizeof(struct Node));//creating a node and storing its adrs in pointer 'temp' – ABHISHEK KUMAR Sep 13 '15 at 09:07
  • The `error` message that you are getting, when you are trying to `compile` – Haris Sep 13 '15 at 09:08
  • compiled without typecasting,getting an error: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] struct Node* temp =malloc(sizeof(struct Node)); – ABHISHEK KUMAR Sep 13 '15 at 09:11
  • 1
    @ABHISHEKSRIVASTAVA use header `` to remove this warning. – ameyCU Sep 13 '15 at 09:13
  • Thanks ameyCU .Plz explain why we used it. – ABHISHEK KUMAR Sep 13 '15 at 09:20
  • @ABHISHEKSRIVASTAVA Function `malloc` is defined in this header . – ameyCU Sep 13 '15 at 09:24
  • @ameyCU Thanks.Can you site any resource to study about structure and pointers. – ABHISHEK KUMAR Sep 13 '15 at 09:27
  • @ABHISHEKSRIVASTAVA For standard reference -http://www.cplusplus.com/reference/ . for working programs -http://www.geeksforgeeks.org/c/. – ameyCU Sep 13 '15 at 09:33
  • @ABHISHEKSRIVASTAVA But mainly I found **Stackoverflow** very helpful . Regular visiting and Q & A's are vary helpful. – ameyCU Sep 13 '15 at 09:34
  • Please have a look here: http://stackoverflow.com/q/562303/694576 Have your choice(s), read and come back. – alk Sep 13 '15 at 10:14

1 Answers1

1

You should write

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

instead of

 struct Node* temp = (Node*)malloc(sizeof(struct Node));

because struct tags are in a different namespace than usual identifiers. So the struct keyword is necessary to specify that.Read this

Moreover include <stdlib.h> else you will receive a warning that you are implicitly declaring malloc.

Community
  • 1
  • 1
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29