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.
--
Compiler OutputCode:#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; }
--
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.