Why am I getting this error?

This is a discussion on Why am I getting this error? within the C Programming forums, part of the General Programming Boards category; C:\Program Files\Miracle C\queue_insert.c: line 3: Parse Error, expecting `'{'' 'struct node' aborting compile This is the code I don't see ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    12

    Why am I getting this error?

    C:\Program Files\Miracle C\queue_insert.c: line 3: Parse Error, expecting `'{''
    'struct node'
    aborting compile

    This is the code I don't see the problem with it.

    #include <stdio.h>

    struct node;

    struct node
    {
    char value;
    node *next;
    };

    node *head=NULL, *tail=NULL;

    void addNode(char);
    void printList();

    void main()
    {
    char input;

    printf("Enter you name:\n");
    input = getchar();
    while( input != '\n' )
    {
    addNode( input );
    input = getchar();
    }

    printf("\nYou have entered: ");
    printList();

    printf("\n\nProgram complete\n");
    }

    void addNode( char val )
    {
    node *insert;

    insert = new node;
    insert->value = val;
    insert->next = NULL;

    if( head == NULL ) //if the list is empty
    head = tail = insert;
    else
    {
    tail->next = insert;
    tail = insert;
    }
    }

    void printList()
    {
    node *p = head;

    while( p != NULL )
    {
    printf("%c", p->value );
    p = p->next;
    }
    }

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Flex,

    Remove the line: struct node; and try again. Structures do not use prototypes.
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    Didn't seem to make a differance. But thanks anyway.

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I've taken a longer look at it. Creating a new variable of type 'node' requires the struct keyword.
    Code:
    node *head=NULL, *tail=NULL; /* Incorrect */
    
    struct node *head=NULL, *tail=NULL; /* Correct */
    Also, the keyword new is C++, not C. Try using malloc() or posting in the C++ forum.
    Jason Deckard

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Here, this compiles without error (I leave it up to you to discover any run-time anomalies):
    Code:
    #include <stdio.h>
    
    struct node
    {
      char value;
      struct node *next;
    };
    
    struct node *head=NULL, *tail=NULL;
    
    void addNode(char);
    void printList();
    
    int main( void )
    {
      char input;
    
      printf("Enter you name:\n");
      input = getchar();
      while( input != '\n' )
      {
        addNode( input );
        input = getchar();
      }
    
      printf("\nYou have entered: ");
      printList();
    
      printf("\n\nProgram complete\n");
    }
    
    void addNode( char val )
    {
      struct node *insert;
    
      insert = (struct node *) malloc( sizeof(struct node) );
      insert->value = val;
      insert->next = NULL;
    
      if( head == NULL ) //if the list is empty
        head = tail = insert;
      else
      {
        tail->next = insert;
        tail = insert;
      }
    }
    
    void printList()
    {
      struct node *p = head;
    
      while( p != NULL )
      {
        printf("%c", p->value );
        p = p->next;
      }
    }
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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