Make node code - right?

This is a discussion on Make node code - right? within the C Programming forums, part of the General Programming Boards category; Code: Node *MakeNode(StackE item) { Node *nptr; if( (nptr = malloc ( sizeof nptr) ) != NULL) { nptr = ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Make node code - right?

    Code:
    Node *MakeNode(StackE item)  
    {
      Node *nptr;
      if( (nptr = malloc ( sizeof nptr) ) != NULL)     
            {
            nptr = item;     
            return item;
            }
            else
                    return NULL;
    
    } 
    
    the structs for this are:
    
    typedef int StackE;
    
    typedef struct node{
    StackEntry entry;
    struct node *next;
    }Node;

    Returns: NULL if out of memory; otherwise a ptr to enough memory to hold a Node, with that node set to correct value(s), ready to be put on stack

    Thanks

  2. #2
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Code:
    typedef int StackE;
    
    Node *MakeNode (StackE item)
    {
    Node *nptr = NULL;
    
    if (nptr = malloc (sizeof (Node)))
       nptr->entry = item;
    
    return nptr;
    }
    Last edited by PutoAmo; 03-24-2002 at 01:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 06:54 PM
  2. 8 Puzzle game solver with the Best-First algoritm
    By LordMX in forum C Programming
    Replies: 17
    Last Post: 08-11-2008, 10:00 AM
  3. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 05:35 AM
  4. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 07:52 AM
  5. I need help~~Hash~~THX!
    By freefallin in forum C Programming
    Replies: 5
    Last Post: 04-22-2004, 07:50 AM

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