Solved

Casting

Posted on 1998-09-15
3
284 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

749 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