Solved

Casting

Posted on 1998-09-15
3
283 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

861 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