Solved

Casting

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 71
Socket Programming (Unix) 8 119
Copy output image from TWindowsMediaPlayer 6 23
Installshield for Embarcadero EX 10.1 Berlin 4 20
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now