Solved

Casting

Posted on 1998-09-15
3
282 Views
Last Modified: 2010-04-02
Why does this work in C, but not C++? When compiled as a .c-file, it works, but not as a C++-file.

Atype A;
A = func();

It says can't cast (void *) to (void struct *)    ... or something like that.
If you can't solve it now, I can give some more hints.
Mike
0
Comment
Question by:mdoland
  • 2
3 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1172719
The error you described says 'cannot cast (void*) to (void struct *)'
So i think Atype is a typedef to a struct pointer, for example
struct A {
.
};
typedef struct A *Atype;

C allows pointer casts with no limits, at least at the standard warning level. That means
you can cast a float* to a char* without a (char*) before the float.
Under C++, this is treated much more critical as under C ... I think because too many errors
resulted of such typecasts.

P.S: In my personal opinion, it's not good to read if you typedef pointers without
some hints ... for example, 'typedef struct A *Atype' -> no one will see that 'Atype' is a pointer
without seeing the typedef line. But 'typedef struct A *pAType' gives some hint that the
'pAType' type refers to a pointer :)
0
 
LVL 11

Accepted Solution

by:
alexo earned 60 total points
ID: 1172720
In C, a "general" pointer (void*) can be IMPLICITLY converted to any type of pointer.  Thus:
    char* p = malloc(5); /* Legal C */

However, C++ requires an EXPLICIT conversion, thus:
    char* p = (char*) malloc(5); // Legal C++
    char* p = static_cast<char*>(malloc(5)); // Better C++

Given the usial declaration: void* malloc(size_t);

But then:
    char* p = new char[5]; // Best C++

0
 
LVL 11

Expert Comment

by:alexo
ID: 1172721
And the autograder hits again!
Thank you *so much* for your consideration!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question