linked list errors

This is a discussion on linked list errors within the C Programming forums, part of the General Programming Boards category; i am running this program Code: #include<stdio.h> struct node { int data; struct node *ptr; } int a,i=0; main() { ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    linked list errors

    i am running this program
    Code:
    #include<stdio.h>
    struct node
    {
      int data;
      struct node *ptr;
    }
    int a,i=0;
    main()
    {
       int o;
      struct node *tmp,*lob,*pnt;
      tmp=(struct node *)malloc(sizeof(struct node));
      lob=(struct node *)malloc(sizeof(struct node));
      pnt=(struct node *)malloc(sizeof(struct node));
    
     while(1)
    {
     printf("Whether you want to add data\n" 
            "1.yes\n"
            "2.print\n"
            "3.exit                :");
    scanf("%d",&o);
    switch(o)
    {
    case(1):
    {   
    add();
    break;
    }
    case(2):
    {   
    print();
    break; 
    }
    case(3)
    exit(1);
    }
    }
    }
    add()
      {
    scanf("%d",&a);
    lob->data=a;
    lob->ptr=NULL;
    tmp->ptr=lob;
    tmp=lob;
    if(i<1)
    {
    pnt=tmp;
    i++;
    }
    }
    print()
      {
    while(pnt->data==NULL)
    {
    printf("%d\n",pnt->data);
    pnt=pnt->ptr;
    }
    i am getting these errors
    Code:
    linkedlist.c:7: error: two or more data types in declaration specifiers
    linkedlist.c:7: error: invalid initializer
    linkedlist.c: In function ‘main’:
    linkedlist.c:12: warning: incompatible implicit declaration of built-in function ‘malloc’
    linkedlist.c:36: error: expected ‘:’ or ‘...’ before ‘exit’
    linkedlist.c:36: warning: incompatible implicit declaration of built-in function ‘exit’
    linkedlist.c: In function ‘add’:
    linkedlist.c:42: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘struct node *’
    linkedlist.c:43: error: ‘lob’ undeclared (first use in this function)
    linkedlist.c:43: error: (Each undeclared identifier is reported only once
    linkedlist.c:43: error: for each function it appears in.)
    linkedlist.c:45: error: ‘tmp’ undeclared (first use in this function)
    linkedlist.c:47: error: invalid operands to binary < (have ‘struct node’ and ‘int’)
    linkedlist.c:49: error: ‘pnt’ undeclared (first use in this function)
    linkedlist.c:50: error: wrong type argument to increment
    linkedlist.c: In function ‘print’:
    linkedlist.c:55: error: ‘pnt’ undeclared (first use in this function)
    could anyone please help me what is the error

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'll do the first few:
    Code:
    linkedlist.c:7: error: two or more data types in declaration specifiers
    linkedlist.c:7: error: invalid initializer
    Probably because you are missing a semi-colon here:
    Code:
    struct node
    {
      int data;
      struct node *ptr;
    };
    int a,i=0;
    Code:
    linkedlist.c: In function ‘main’:
    linkedlist.c:12: warning: incompatible implicit declaration of built-in function ‘malloc’
    You need to #include <stdlib.h>.
    Code:
    linkedlist.c:36: error: expected ‘:’ or ‘...’ before ‘exit’
    linkedlist.c:36: warning: incompatible implicit declaration of built-in function ‘exit’
    Exit() is also in stdlib. I'll let you make an educated guess about the "expected :" here
    Code:
    case(3)
    exit(1);
    Now, learn to indent properly please! For your own sake, and everyone elses.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3
    Thanks for your help. Here i have one doubt.
    Code:
      struct node *tmp,*lob,*pnt;
      tmp=(struct node *)malloc(sizeof(struct node));
      lob=(struct node *)malloc(sizeof(struct node));
      pnt=(struct node *)malloc(sizeof(struct node));
    why cant we declare struct pointer variables declaration and allocating memory globally?why it needs to come inside main()

    if i have to use all the memory allocation for struct pointer variables inside main() means it wont work in display().in this case whether i have to declare again in display()?
    Last edited by shariefbe; 05-05-2010 at 05:47 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can declare variables globally. But you can only run code inside a function, and malloc is a function call and therefore code.

    Functions take parameters for a reason, and "this function needs to know about these variables" is that reason.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    ok fine. now my doubt is if i run that "malloc" code inside main(),whether is it necessary to allocate memory again in display()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. Linked List errors, and need help adjusting.
    By 1BadRbt in forum C Programming
    Replies: 2
    Last Post: 01-06-2007, 11:22 PM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21