473,797 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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*)vo id_pointer;
// void_pointer is of type void*
if(foo == NULL)
{
printf("Error!\ n");
}

/ William Payne
Nov 14 '05 #1
4 9313
William Payne wrote:
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*)vo id_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
--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #2

On Sat, 14 Feb 2004, William Payne wrote:

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*)vo id_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

Nov 14 '05 #3
Artie Gold wrote:
William Payne wrote:
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*)vo id_pointer;

Do you perhaps mean:

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

= (of course)
// 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

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #4
Arthur J. O'Dwyer <aj*@nospam.and rew.cmu.edu> spoke thus:
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*)vo id_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;


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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2539
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
11
3897
by: Roman Mashak | last post by:
Hello, All! I'm implementing function parsing config file, which look like this: # this is comment port=10000 path=/var/run/dump.pid .... Declared the type
9
2728
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: 0x00000000 through 0xFFFFFFFE : Valid byte addresses
6
527
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a direct way to perform this cast? If not, is there way in C++ to do it? For example, I would like my function test() to work the way it is coded below. It produces a compiler error though: #include <pshpack2.h// 16-bit padding
6
2654
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
10
7989
by: Allen | last post by:
I want to package an address to byte buffer. So I need to cast it to integer value. How to cast it? const char * ptr = 0x0013e328; int value = <cast(ptr); // use reinterpret_cast?
1
452
by: etienne | last post by:
Hello all, i'm experimenting a problem in trying to cast a char * variable to a pointer to a function. My problem is that I want to execute functions in threads, using the pthread_create function. The name of the functions are read in txt file at runtime. So I want to do something like char * func_to_run = scanf(the, good, args); pthread_create (thread, attr, (magic cast)funct_to_run, arg);
14
4440
by: andreyvul | last post by:
g++ says a reinterpret cast from void* is disallowed; however, all I'm trying to do is de-capsulate a void* argument that holds the pointer to a class (and static_cast forces cast constructor) any solutions? also this pops up: 43: error: expected unqualified-id before 'return' code: includes: stdio.h, stdlib.h, time.h, pthread.h struct: template <typename Tstruct mergesort_thread { T* start;
7
3951
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.