What's wrong with this piece of code?

This is a discussion on What's wrong with this piece of code? within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 06:58 PM
  3. whats wrong with this piece of code?
    By psycho88 in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 10:28 PM
  4. Help with a piece of code
    By Victor4015 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2005, 04:38 PM
  5. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 11:38 AM

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