Thread: Invalid conversion from void to char

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    46

    Invalid conversion from void to char

    Hey guys, I have a problem with this code not compiling. The compiler says that the bolded line is an invalid conversion from void to char but I don't understand why this won't work. Thanks all!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void quiz(char *s);
    
    void quiz(char *s)
    {
    
         char *t = malloc(strlen(s) + 1);
    
         char *p = t;
    
         while (*s)
         {
               if (!('a' <= *s && *s <= 'z'))
               {
    
                  *p = *s;
    
                  p++;
               }
    
               s++;
         }
    
         *p = '\0';
    
         printf("%s",t);
    }
    
    int main()
    {
          char array[] = "I heart CS262";
          
          quiz(array);
    
          return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest using a newer C Compiler.

    Or, using the old way of casting the return of malloc.

    Tim S.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You are probably compiling as C++ rather than C.

    AAAAANNNNDDDD...cross-posted

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you can cast it, check to make sure it's not being compiled as C++ code, or get a newer Compiler as stahta01 suggested
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Quote Originally Posted by rags_to_riches View Post
    You are probably compiling as C++ rather than C.

    AAAAANNNNDDDD...cross-posted
    Oh, thank you sir.

    Also, this exam is in an hour and I am just trying to get answers ASAP.

    Thanks for all the help guys!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Also, this exam is in an hour and I am just trying to get answers ASAP.
    In other words, it was "urgent".

    How To Ask Questions The Smart Way
    Not our problem.

    Try to imagine a world where every selfish person in a hurry cross-posts the SAME thing on many different forums.
    How much of the limited FREE time that helpers provide is going to be spent on wasted duplicate effort?
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by askinne2 View Post
    Oh, thank you sir.

    Also, this exam is in an hour and I am just trying to get answers ASAP.

    Thanks for all the help guys!
    Do I understand you to say you're sitting in an exam as you post this?

    WOW... cheating has just moved to a whole new level.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My code wont link with gcc but does with Visual Studio
    By moniker in forum C Programming
    Replies: 2
    Last Post: 12-19-2009, 05:08 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  5. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM