Linker List stuck! even before entering

This is a discussion on Linker List stuck! even before entering within the C Programming forums, part of the General Programming Boards category; I try to do something called linked list but just before i dive in i'm stuck. Plz help Code: #include ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Linker List stuck! even before entering

    I try to do something called linked list but just before i dive in i'm stuck. Plz help
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{	//Struct node type
    	int data;
    	struct node *ptr;
    };
    
    typedef struct node Node;
    
    	Node newPtr;
    	Node *sPtr=NULL;
    
    
    int InsNewAtFirst(int value);//Function Prototype
    
    int main(){
    	
    
    	
    	InsNewAtFirst(100);
    	InsNewAtFirst(50);
    	InsNewAtFirst(150);
    	InsNewAtFirst(450);
    	InsNewAtFirst(5150);
    	getchar();
    }
    
    int InsNewAtFirst(int value){
    	
    	newPtr=malloc(sizeof(Node)); //stuck!  cannot convert 'void*' to 'Node'
    };

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You defined the variable newPtr as "Node", which is not a pointer, hence the compiler message.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    so how to fix it? I need newPtr as Node to being use as a node to store a data and a pointer.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You defined newPrt as a static variable: the memory for it is already allocated. So either this is what you want and it's not a pointer, or you do want a pointer and you need to declare it as such (like you've done for sPtr).
    To sum up (and simplify) the memory for every variable you declare is already allocated for you, except if you declare it with as a pointer (*), in which case you need to allocate it manually. If you apply this rule, you see that what you've done is incorrect.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    For linked lists you almost certainly want to use pointers, and I think that's what you intended since you called malloc. So:

    Code:
        Node *newPtr;
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on a linked list
    By projektaquarius in forum C++ Programming
    Replies: 16
    Last Post: 11-16-2006, 01:02 AM
  2. ,getting the key without ENTERing
    By fnoyan in forum Linux Programming
    Replies: 1
    Last Post: 03-06-2006, 06:22 AM
  3. long input list for Turbo linker
    By AltCtrlDel in forum C Programming
    Replies: 2
    Last Post: 07-09-2005, 12:35 AM
  4. Entering strings into a list?
    By chadsxe in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2005, 12:40 PM
  5. entering numbers into a linked list
    By swiftfire in forum C Programming
    Replies: 2
    Last Post: 12-04-2002, 12:16 PM

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