473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

void* to char* ?

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
11 3609

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
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
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
"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
"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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Olaf Martens | last post by:
Greetings! Here's another problem that appeared recently: I have the following function call: l_test->Compress(&"filename.dat",l_buf); l_test is a pointer-to-packer that I want to check...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
6
by: rouble | last post by:
Hi All, Is it safe to store a uchar in a void* and then extract the uchar value out of it again ? My understanding is that the size of a void* should always be equal to or greater than the...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
7
by: bwaichu | last post by:
In some APIs, we see char ** or void ** as a parameter. I never distinguished between declaring a variable as char **x and passing x as the parameter from declaring a variable char *x and passing...
18
by: planetzoom | last post by:
Given the following code: #include <stdio.h> int main(void) { char array = "What is your favorite car?"; void *vp = &array; printf("%s\n", vp);
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
28
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.