Thread: What's wrong with this piece of code?

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32

    What's wrong with this piece of code?

    Here's the code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    struct node
    {
           int data;
           struct node *next;
    }
    void push(struct node **head_ref, int value)
    {
         struct node *new_node = (struct node*)malloc(sizeof(struct node));
         new_node->data = value;
         new_node->next = (*head_ref);
         (*head_ref) = new_node;
    }
    int main(void)
    {
        struct node *head = NULL;
        push(&head, 10);
        getch();
        return 0;
    }
    On compilation, I get this error: "two or more data types in declaration of 'push'.

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're suffering from the same problem as this poster.
    Error in an array of structures program

    > struct node *new_node = (struct node*)malloc(sizeof(struct node));
    Instead of casting, try
    #include <stdlib.h>
    instead, and remove the cast.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    Thanks Salem, figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with this piece of code?
    By shaxquan in forum C Programming
    Replies: 3
    Last Post: 06-14-2010, 04:46 AM
  2. what's wrong with this piece of code
    By studentc in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2006, 07:58 PM
  3. whats wrong with this piece of code?
    By psycho88 in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:28 PM
  4. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 05:38 PM
  5. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM