If a cast from void* to a struct type pointer fails

W

William Payne

Hello!
If a cast from a void pointer to a pointer to a struct fails at runtime, is
NULL the result? I.E, is this code meaningful:

struct my_struct_t foo* = (struct my_struct_t*)void_pointer;
// void_pointer is of type void*
if(foo == NULL)
{
printf("Error!\n");
}

/ William Payne
 
A

Arthur J. O'Dwyer

If a cast from a void pointer to a pointer to a struct fails at
runtime, is NULL the result? I.E, is this code meaningful:

struct my_struct_t foo* = (struct my_struct_t*)void_pointer;

First of all, it's not correct, because there are syntax errors
on it. Fixing the errors, and getting rid of the spurious cast
(which serves only to shut up the compiler about errors; casts
almost never solve problems), we have

| struct my_struct_t *foo = void_pointer;

If 'void_pointer' is pointing at a 'struct my_struct_t', or is
a null pointer, then this will work in the obvious fashion.
Otherwise, this line invokes undefined behavior. For example,

int i;
void *p = &i;
struct my_struct_t *foo = void_pointer;

This is undefined behavior and can do anything it likes (including
possibly setting 'foo' to NULL, but more likely setting it to a
nonsense value, rounding it to a "close-enough" value, or raising
a bus error).

struct my_struct_t s;
void *p = &s;
struct my_struct_t *foo = void_pointer;

This is defined behavior, and 'foo' now points to 's'.

HTH,
-Arthur
 
A

Artie Gold

William said:
Hello!
If a cast from a void pointer to a pointer to a struct fails at runtime, is

What do you mean by `fails'?
NULL the result? I.E, is this code meaningful:

struct my_struct_t foo* = (struct my_struct_t*)void_pointer;

Do you perhaps mean:

struct my_struct_t *foo - (struct my_struct_t *)void_pointer;
// void_pointer is of type void*
if(foo == NULL)
{
printf("Error!\n");
}

With the correction above, sure -- iff `void_pointer' was NULL to begin
with and originated as a pointer to `struct my_struct_t'.

I think you either have another question here -- or it's time to OTDB!*

HTH,
--ag

* - Open The Damn Book
 
C

Christopher Benson-Manica

First of all, it's not correct, because there are syntax errors
on it. Fixing the errors, and getting rid of the spurious cast
(which serves only to shut up the compiler about errors; casts
almost never solve problems), we have
| struct my_struct_t *foo = void_pointer;

Is it possible that the OP was thinking in the C++ world, which (IIRC)
requires the cast of void_pointer? Perhaps he was looking for
something like dynamic_cast that throws an exception when it "fails"
at runtime? Just a (probably wrong and definitely OT) thought.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top