Thread: cast error

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    cast error

    Hey guys im gettin an assignment makes integer from pointer without a cast?
    im trying to store anInt into the data


    Code:
    new->data = anInt;
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    
    typedef struct listNode
    {
      int data;
      struct listNode * next;
      	
    }ListNode;
    
    typedef ListNode * ListNodePtr;
    
    int main(void)
    {
    	ListNodePtr head, new, current, previous, next;
    	
    	unsigned listSize;
    	int i, anInt;
    	
    	listSize = 0;
    	head = NULL;
    	
    	while(scanf("%d", &anInt) == 1)
    	{
    		if((new = malloc(sizeof(ListNode)))==NULL)
    		{
    		  fprintf(stderr,
    		  "\nmemory allocation forlist insert failed\n");	
    		   fprintf(stderr, "aborting data entry\n");
    		   break;
    		}
    		
    		current  = head;
    		previous = NULL;
    		
    		while(current !=NULL && current->data < anInt)
    		{
    		  previous = current;
    		  current = current->next;	
    		}
    		
    		new->data = anInt;
    		new->data = current;
    		listSize++;
    		
    		if(previous == NULL)
    		{
    		  head = new;	
    		}
    		else
    		{
    		  previous->next = new;	
    		}
    	}/*end of major while loop*/
    	
    	/*display integers in linked list*/
    	current = head;
    	while(current !=NULL)
    	{
    	   printf("%d\n", current->data);
    	   current = current->next;	
    	}
    	
    	/*deallocate memory here*/
    	current = head;
    	while(current !=NULL)
    	{
    	  next = current->next;
    	  free(current);
    	  current = next;	
    	}
    	return 	EXIT_SUCCESS;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    		new->data = current;
    Code:
    typedef struct listNode
    {
      int data;
      struct listNode * next;
      	
    }ListNode;
    Code:
    	ListNodePtr head, new, current, previous, next;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Incidentally, there's nothing wrong with it in C, per se, but using C++ keywords in C like new is generally not a good idea if you ever have to port your code over.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Yehhhh this was a tutorial question outta the book we had to compile it without errors crappy variable names

    so i dont get why it still wnt compile without errors?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post actual error messages, and indicate which source lines those correspond to.
    Post actual command lines (you're using a C compiler and not a C++ compiler right?)
    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.

  6. #6
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Im writing that in Ansi C compiler. Its line 44 which is causing the cast error not sure why?

    gcc -ansi -Wall -pedantic -o filename filename.c

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Did you read Dave_Sinkula's post, the first reply in this thread? You're assigning a ListNodePtr to an int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM