Solved

Casting

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

707 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

19 Experts available now in Live!

Get 1:1 Help Now