error C2440 grrrrrrrr

C

carmelo

Hi! I wrote this function that shoul read dinamically a string, but
the compiler gimes me this error:
error C2440: '=' : cannot convert from 'void *' to 'char *'
Why?

char *read_string(void)
{
int i=1;
char *string, c;

string=malloc(sizeof(char)); <----------------------- here
while(isspace(c=getchar()));
do
{
string = realloc(stringa,++i*sizeof(char));
<----------------------- here
string[i-2]=c;
string[i-1]='\0';
}
while(!(isspace(c=getchar())));
return stringa;
}
 
V

Victor Bazarov

carmelo said:
Hi! I wrote this function that shoul read dinamically a string, but
the compiler gimes me this error:
error C2440: '=' : cannot convert from 'void *' to 'char *'
Why?

char *read_string(void)
{
int i=1;
char *string, c;

string=malloc(sizeof(char)); <----------------------- here

Don't do that. Read about, and use, the 'new' operator. It's
the 21st century, get on with the program.

Well, the error, of course, is because 'void*' (returned by
'malloc') is not convertible to 'char*' (the type of 'string'
in your code), you need to use 'static_cast'.
while(isspace(c=getchar()));
do
{
string = realloc(stringa,++i*sizeof(char));
<----------------------- here

Again, don't use 'realloc'. Are you writing C++ or C? If you
are writing C++, use 'std::string' class. If you *are* writing
C, God bless, off you go to comp.lang.c newsgroup...
string[i-2]=c;
string[i-1]='\0';
}
while(!(isspace(c=getchar())));
return stringa;
}

V
 
P

peter koch

Hi! I wrote this function that shoul read dinamically a string, but
the compiler gimes me this error:
error C2440: '=' : cannot convert from 'void *' to 'char *'
Why?

char *read_string(void)
{
        int i=1;
        char *string, c;

Don't have more than one variable per declaration - that is confusing.
        string=malloc(sizeof(char));      <-----------------------  here

Also try to initialise as you declare. You can't do that for ch, but
you can for string.
Notice also that you do not check for error-condition which is kind-of
silly although I agree that it probably would not matter here.
        while(isspace(c=getchar()));
        do
        {
                string = realloc(stringa,++i*sizeof(char));´
Here and in the return statement, you reference an unknown variable
"stringa". If this compiles, your program will not work! Also, while
I'm not familiar with getchar, I am not sure that your program will
work in case of an error while calling getchar.

Least important, your program is O(n*n) which is clearly suboptimal.
An optimal program is O(n).
<-----------------------  here
                string[i-2]=c;
                string[i-1]='\0';
        }
        while(!(isspace(c=getchar())));
        return stringa;



}

A program written in C++ would use std::string, std::cin (or whatever
getchar uses), would be a one-liner and avoid all of the problems
above. Since you are posting in a C++ forum, I propose that you follow
that approach.

/Peter

PS: The problem is that malloc and realloc returns void*. In C++ there
is no implicit conversion to char*.
 
J

Juha Nieminen

carmelo said:
Hi! I wrote this function that shoul read dinamically a string

Wouldn't it be much easier to do it like this:

std::string s;
std::cin >> s;

(And if you *really* need a char* from that, s.c_str().)
string=malloc(sizeof(char)); <----------------------- here

This is simply horrible C++ and you should not write code like that.
Use std::string instead.

Anyways, the error is that C++ doesn't allow implicit casts from void*
to other types, so you have to cast explicitly.
 
J

Jeff Schwab

carmelo said:
Hi! I wrote this function that shoul read dinamically a string, but
the compiler gimes me this error:
error C2440: '=' : cannot convert from 'void *' to 'char *'
Why?

char *read_string(void)
{
int i=1;
char *string, c;

string=malloc(sizeof(char)); <----------------------- here
while(isspace(c=getchar()));
do
{
string = realloc(stringa,++i*sizeof(char));
<----------------------- here
string[i-2]=c;
string[i-1]='\0';
}
while(!(isspace(c=getchar())));

(Did you mean to read past the end of your non-whitespace sequence? If
you're writing C code, it might be better etiquette to push the
terminating space (if any) back onto the stream with putchar.)

return stringa;
}

"Grrrrrrrr" indeed. :)

As others have pointed out, that's C, not C++. You can push it through
a C++ compiler if you need to, by casting the result of malloc to char*,
but you're probably better off re-writing the code in modern C++.
Roughly equivalent C++ code to the above would be:

#include <iostream>
#include <string>

std::string read_string() {
std::string s;
std::cin >> s;
return s;
}

However

(1) C++ functions are not generally hard-coded to read from particular
streams. Instead, they typically are passed a std::eek:stream&.

(2) "Read" functions are often best implemented as extraction operators
for given types:

std::eek:stream& operator>>(std::eek:stream&, my_type&);

(3) The standard string type already has such an operator, and the
standard stream is convertible to a type usable as a loop test
condition. Simple, one-off filter programs often look like this:

#include <iostream>
#include <string>

int main() {

std::string s;

while (std::cin >> s) {
// ...
}

return !std::cin.eof();
}

No dedicated read_string function is required.
 
J

James Kanze

carmelo wrote:

[...]
Again, don't use 'realloc'. Are you writing C++ or C? If you
are writing C++, use 'std::string' class. If you *are* writing
C, God bless, off you go to comp.lang.c newsgroup...

And compile the code with a C compiler, not with a C++ compiler.
Fundamentally, his problem is that he's compiling a program
written in C with a C++ compiler. C is considerably laxer than
C++ with regards to type checking. (Of course, his code isn't
correct C either. But there's nothing in it which would cause a
compiler to complain---just undefined behavior.)
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top