Thread: malloc(), um.. yeah. This should work, but it isn't.

  1. #1
    0x01
    Join Date
    Sep 2001
    Posts
    88

    malloc(), um.. yeah. This should work, but it isn't.

    Compiler : Microsoft Visual C++ (6.0)
    OS : Windows XP Professional
    --

    I was just wanting to test the malloc() function.. Well, I got an error when trying to compile the following.
    --

    The program is supposed to allocate memory on-the-fly for a string of 35(includeing the \0) chars or less, then assigns the alphabet(Uppercase) to the memory location, and then prints it to the screen.
    --

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    int main()
    {
    	char count;
    	char * ptr, *p;
    
    	ptr = malloc(35 * sizeof(char));
    
    	p = ptr;
    
    	for(count = 65; count < 91; count++)
    		*p++ = count;
    
    	*p = '\0';
    
    	puts(ptr);
    
    	return 0;
    }
    Compiler Output
    --
    error C2440: '=' : cannot convert from 'void *' to 'char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    --

    I disagree with this, I don't see anything wrong with the above code... Its very frustrating to look for an error in code, when there is none. Can anyone tell me whats wrong with this?

    --
    The code above came from SAMS Teach Yourself C in 21 Days (Fifth Edition)
    --

    Yeah, i know this is a boring question/thread.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, try what it suggests, use an explicit cast:

    Code:
    ptr = (char*) malloc( 35*sizeof(char) );
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    It's because it's compiling it as C++ code. Change the file extension (manually) to .c, and recompile.

    Either that, or change this:

    ptr = malloc(35 * sizeof(char));

    to this:

    ptr = (char *)malloc(35 * sizeof(char));

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Compiles and executes with no problems for me, using msvc 6.
    ptr = malloc(35 * sizeof(char));
    isnt a char always 1 byte? So just the 35 would do?
    Also check for success
    Code:
    ptr = malloc(35);
    if(!ptr)
       printf("No more memory");
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    0x01
    Join Date
    Sep 2001
    Posts
    88
    ptr = (char*) malloc( 35*sizeof(char) ); // worked perfect.

    >>It's because it's compiling it as C++ code. Change the file extension (manually) to .c, and recompile.

    so...
    ptr = malloc(35 * sizeof(char)); is for C++

    and...
    ptr = (char *)malloc(35 * sizeof(char)); is for C

    hm.. I didn't know that.
    --

    >>isnt a char always 1 byte? So just the 35 would do?
    Well for portability reasons, you would use the sizeof() macro(i think its a macro).
    --

    Thanks to all.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, actually you have it backwards. C does not require you to typecast void pointers, C++ does.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  2. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  3. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM