void* to char* ?

B

bildad

I like fixing things so I'm working on programs from books that came in a
package deal with Borland C++ 4.52. I'm trying to fix them to compile with
gcc 3.4.2. I don't understand what's causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*' to `char*'

/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}
 
C

Chris Hulbert

bildad said:
I like fixing things so I'm working on programs from books that came in a
package deal with Borland C++ 4.52. I'm trying to fix them to compile with
gcc 3.4.2. I don't understand what's causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*' to `char*'

/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}

gcc is probably compiling it as a C++ program in which case C++
requires explicit conversions from void * (i.e. you have to cast the
malloc). You should make sure the extension is .c as gcc usually
determines by the filename the kind of source file and therefore which
compilers to use.
 
C

Christopher Benson-Manica

bildad said:
/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>
#define MAX 100
int main(void) // was void main(void)

Good.
{
char * string;
string = malloc( MAX );

You should check to ensure that malloc() succeeded:

if( !string ) {
fprintf( "Malloc failed" ); /* or whatever */
exit( EXIT_FAILURE );
}
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()

Well-intentioned but wrong. string is a pointer; sizeof(string) is
the size of the pointer, not the size of the memory it points to (if
indeed it points to any).

fgets( string, MAX, stdin );
 
K

Keith Thompson

bildad said:
I like fixing things so I'm working on programs from books that came in a
package deal with Borland C++ 4.52. I'm trying to fix them to compile with
gcc 3.4.2. I don't understand what's causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*' to `char*'

/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}

Apparently the malloc call is on line 10, but that's not obvious
without counting lines. Adding a "/* line 10 */" comment would have
been helpful.

The program above is valid C, but invalid C++. gcc assumes that a
file with a ".C" suffix is C++. Rename the file with a ".c" suffix.
 
M

Mark B

bildad said:
I like fixing things so I'm working on programs from books that came in a
package deal with Borland C++ 4.52. I'm trying to fix them to compile with
gcc 3.4.2. I don't understand what's causing the following error but I
think it might have something to do with malloc(). Any insight
appreciated.

DAY01E4.C:10: error: invalid conversion from `void*' to `char*'

give your module a '.c' extension, not '.C' (indicates c++)
/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
string = malloc( MAX );

You should ensure the call succeeded before using 'string'
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()

You may also want to replace 'sizeof(string)' with something
other than the size of the pointer :)
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}

HTH
 
A

A. Sinan Unur

give your module a '.c' extension, not '.C' (indicates c++)

What might be confusing on a DOS like system is that even if the file is
called file.c, if you type

gcc -c FILE.C

on the command line, it will be find and compile it (but it will assume
that it is C++). So, if you want to compile as C, make sure you call
invoke gcc as

gcc -c file.c

on a DOS-like system.

Sorry for the off-topic post.

Sinan
 
J

Jordan Abel

I like fixing things so I'm working on programs from books that came in a
package deal with Borland C++ 4.52. I'm trying to fix them to compile with
gcc 3.4.2. I don't understand what's causing the following error but I
think it might have something to do with malloc(). Any insight appreciated.

DAY01E4.C:10: error: invalid conversion from `void*' to `char*'

The problem is that Bjarne Stroustrup doesn't understand the purpose
of void pointers, and this is reflected in the c++ language
definition.
/* Day 1: Exercise 4 */
#include <stdlib.h>
#include <stdio.h>

#define MAX 100

int main(void) // was void main(void)
{
char * string;
#ifdef __cplusplus
string = (char *)malloc(MAX);
#else
string = malloc( MAX ); #endif
printf( "Enter something: " );
// gets( string );
fgets( string, sizeof(string), stdin ); // to replace gets()
puts( string ); /* do something like printing */
free( string );

return 0; // for int main()
}

you can probably simplify this with a macro if you have to do it a
lot
 
S

Simon Biber

Jordan said:
The problem is that Bjarne Stroustrup doesn't understand the purpose
of void pointers, and this is reflected in the c++ language
definition.



#ifdef __cplusplus
string = (char *)malloc(MAX);
#else



you can probably simplify this with a macro if you have to do it a
lot

If you have a good reason to regularly compile the same code with C and
C++ compilers, then you may as well leave the cast in, and not bother
with preprocessor tricks like the above.

If you will only be compiling the code with a C compiler, then you
should leave out the cast.

If you will only be compiling the code with a C++ compiler, then you
should think about using new[] and delete[] instead.
 
K

Keith Thompson

Simon Biber said:
If you have a good reason to regularly compile the same code with C
and C++ compilers, then you may as well leave the cast in, and not
bother with preprocessor tricks like the above.

If you *think* you have a good reason to regularly compile the same
code with C and C++ compilers, you probably don't. It's possible that
you might, but it almost always makes more sense just to use one
language or the other.
 
M

Mark McIntyre

The problem is that Bjarne Stroustrup doesn't understand the purpose
of void pointers, and this is reflected in the c++ language
definition.

Its very considerably more accurate to say that BS probably felt there
were better ways that a new language could achieve the same effect and
with better type safety. I suspect BS understood what C meant by void
pointers very well indeed.
 
B

bjarne

Mark said:
Its very considerably more accurate to say that BS probably felt there
were better ways that a new language could achieve the same effect and
with better type safety. I suspect BS understood what C meant by void
pointers very well indeed.

I suspect so also :)

Also, C++'s implementation of void* predates the first proposal to the
C standards committee by about half a year.

For some information about the relationship between C and C++, see
http://www.research.att.com/~bs/bs_faq.html#difference
http://www.research.att.com/~bs/bs_faq.html#C-is-subset
http://www.research.att.com/~bs/bs_faq.html#merge
and especially the papers referenced there.

-- Bjarne Stroustrup

 
J

Jordan Abel

I suspect so also :)

Also, C++'s implementation of void* predates the first proposal to the
C standards committee by about half a year.

Sorry if I offended you with my rather blunt statement. In my
defense, I was half-asleep when I posted that, and the attitude
reflected in it originates in freenode/##c , where quite often
people ask about problems in their c++ code without even stating
that it's c++, or claiming that the fact that it's c++ doesn't
matter. Also, based on your papers on improving compatibility
between the two, my thought was that the attitude towards c++ being
'a new language' and thus not bound by any compatibility concerns to
c, while not necessarily a fundamentally incorrect one, was not one
held by you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top