By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

void* to char* ?

P: n/a
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()
}
Nov 15 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

bildad wrote:
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.

Nov 15 '05 #2

P: n/a
bildad <bi****@wi.rr.com> wrote:
/* 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 );
puts( string ); /* do something like printing */
free( string ); return 0; // for int main()
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3

P: n/a
bildad <bi****@wi.rr.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

P: n/a
"bildad" <bi****@wi.rr.com> wrote in message
news:2g****************************@40tude.net...
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
Nov 15 '05 #5

P: n/a
"Mark B" <so***@localbar.com> wrote in
news:Sm******************@monger.newsread.com:
"bildad" <bi****@wi.rr.com> wrote in message
news:2g****************************@40tude.net...
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++)


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

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Nov 15 '05 #6

P: n/a
On 2005-10-27, bildad <bi****@wi.rr.com> wrote:
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
Nov 15 '05 #7

P: n/a
Jordan Abel wrote:
On 2005-10-27, bildad <bi****@wi.rr.com> wrote:
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


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.

--
Simon.
Nov 15 '05 #8

P: n/a
Simon Biber <ne**@ralmin.cc> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9

P: n/a
On Thu, 27 Oct 2005 23:39:03 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #10

P: n/a

Mark McIntyre wrote:
On Thu, 27 Oct 2005 23:39:03 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
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.


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

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


Nov 15 '05 #11

P: n/a
On 2005-10-28, bjarne <bj****@gmail.com> wrote:

Mark McIntyre wrote:
On Thu, 27 Oct 2005 23:39:03 +0000 (UTC), in comp.lang.c , Jordan Abel
<jm****@purdue.edu> wrote:
>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.


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.
Nov 15 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.