invalid conversion What's this?

This is a discussion on invalid conversion What's this? within the C Programming forums, part of the General Programming Boards category; I was on a middle on making my code then I encountered this Code: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct stackNode { ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    66

    invalid conversion What's this?

    I was on a middle on making my code then I encountered this

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    struct stackNode
    {
           int data;
           struct stackNode *nextPtr;
           };
           
          typedef struct stackNode StackNode;
          typedef StackNode *StackNodePtr;
          
          
    void instructions();
    void push(StackNodePtr *, int);
                
                
    int main()
    {
        instructions();
        
        getch();
        }
    
    
      void instructions()
      {
           printf("Please enter a choice\n");
           printf("[1]Push a value on the stack\n");
           printf("[2]Pop a value off the stack\n");
           printf("[3]Display the whole stack\n");
           printf("[4]Exit");
           } 
         
         void push (StackNodePtr *topPtr, int info)
         {
              StackNodePtr newPtr;
              newPtr = malloc(sizeof (StackNode));
              if(newPtr !=NULL)
              {
                        newPtr->data=info;
                        newPtr->nextPtr=*topPtr;
                        *topPtr = newPtr;
                        }
              }
    I am not yet but when I compiled it there's an error popping it help please

  2. #2
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    on which line?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    newPtr = malloc(sizeof (StackNode)); this one :|

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    malloc returns a void pointer. You need to typecast it.

    newPtr =(StackNode *) malloc(sizeof
    (StackNode));

    i think this should work.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Make sure you are using C compiler. Not c++.
    And don't cast malloc return value in C.
    http://c-faq.com/malloc/mallocnocast.html
    Last edited by Bayint Naung; 04-06-2011 at 08:41 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    If your compiler is complaining about an invalid conversion (or words to that effect) then it is a C++ compiler.

    In C++, unlike C, a void pointer cannot be implicitly converted to another pointer type. You will therefore need to either tweak your compiler to compile as C (how you do that, if it is possible, is compiler specific) or use an explicit conversion to stop the compiler moaning.
    Code:
       newPtr = (StackNodePtr)malloc(sizeof(StackNode));
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    I am using a bloodshed C++

    btw before i forget can anyone translate this without typedef?
    Last edited by kyel_dai92; 04-06-2011 at 09:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 06:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 11:30 AM

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