473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

casting pointers

Hi, I've this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;

is legal (or is it?), isn't the (void *) there just line noise?

TIA

Nov 13 '05 #1
8 8864
On Sat, 4 Oct 2003 16:36:48 UTC, rihad <ri***@mail.ru> wrote:
Hi, I've this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;
No: foo = ((void*) ((void*) ((void*) ((void*) ((void*) array)))));

or better another thousend of casts would be neccessary.

is legal (or is it?), isn't the (void *) there just line noise?


No a char array is NOT a struct foo. You can cast float* to unsigned
char - but that makes no more sense than casing a pointer of one type
to another type in general.

And casting a pointer to a type to a pointer void to cast this pointer
to another type makes it no more legal. Because ever when you casts a
pointer to void you're only allowed to cast that pointer _back_ to the
type it was before. A pointer to void is nothing than a container that
hides the original type until you needs that type back.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch wird jetzt ausgeliefert!
Nov 13 '05 #2
rihad <ri***@mail.ru> writes:
Hi, I've this question: suppose we have two differently typed pointers:

struct foo *foo;
char **array;

Becase one can always legally cast any pointer to (void *), and becase (void *)
is assignable to any pointer type, is it ever necessary to cast when assigning
one pointer type to another? I.e. since

foo = (void *) array;


I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.

Casting exists for a very good reason: to protect you from
inadvertant mistakes. The standard could have just decided to
always convert (when possible) to the type of the left-hand
operator to =. I'm damn glad it didn't.

-Micah
Nov 13 '05 #3
On Sat, 04 Oct 2003 21:36:48 +0500, rihad <ri***@mail.ru> wrote:
struct foo *foo;
char **array; <snip>foo = (void *) array;

is legal (or is it?), isn't the (void *) there just line noise?


This construction approaches silliness. The issue is whether the
conversion of array to foo is reasonable. The language allows
conversion between pointers to different objects, but there are issues
of alignment, issues of potential undefined behavior, issues of syntax
violations, and issues of semantics. I would inspect each pointer
conversion carefully to make sure it is what is needed and to make
sure it is sensible.

Best wishes,

Bob
Nov 13 '05 #4
Micah Cowan wrote:
.... snip ...
I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.


No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.

This means that you can use void* as a mechanism to pass pointers
through code that has no idea what it really is or does. Sooner
or later it arrives somewhere where its actual original type is
known, and it can then be restored. This sort of thing lets you
supply compare routines to qsort, which couldn't care less what
the items are, it only needs to know which of two are larger, and
be able to trade pointers to them. However the caller must know
what the items really are, and how to compare them.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5
CBFalconer wrote:

<snip>
You can cast any pointer type to void*, and then you can cast
that back to the original pointer type.
Why bother? You can do the assignment without casting:

SOME_OBJECT_TYPE_OR_OTHER *p = CreateSomeObjectType();
void *v = p;
p = v;

No need for casting.
Casting that void* to a
type other than the original is specifically NOT guaranteed.
True, but neither is not casting it. :-)
This means that you can use void* as a mechanism to pass pointers
through code that has no idea what it really is or does. Sooner
or later it arrives somewhere where its actual original type is
known, and it can then be restored.


Precisely. (And darned useful it is, too.)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6
"CBFalconer" <cb********@yahoo.com> wrote in message
news:3F***************@yahoo.com...
Micah Cowan wrote:

... snip ...

I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.


No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.


The conversion itself is guaranteed explicitly in 6.3.2.3p1, dereferencing
the resulting pointer is a different story.

--
Peter
Nov 13 '05 #7
CBFalconer <cb********@yahoo.com> writes:
Micah Cowan wrote:

... snip ...

I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.


No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.


Yeah, but I was speaking of syntax. It may invoke UB, but it is
guaranteed to *run* (even if the definition of "run" for this
platform is to screechingly halt). It *will* be implicitly
converted (from void*, that is). What happens after that was not
a part of what I was saying: I was speaking strictly in terms of
what guarantees can be made at "compile time" (in standardspeke,
I'm talking about what does and doesn't violate constraints).

-Micah
Nov 13 '05 #8
"Peter Nilsson" <ai***@acay.com.au> writes:
"CBFalconer" <cb********@yahoo.com> wrote in message
news:3F***************@yahoo.com...
Micah Cowan wrote:

... snip ...

I'm not following your logic, which appears to be
non-sequitur. It is *because* you can legally cast any pointer to
(void*), which can then be implicitly converted to the
appropriate type, that the above is correct. It is due to the
fact that the same is *not* guaranteed for other pointer types
that the (void*) isn't just noise.


No. You can cast any pointer type to void*, and then you can cast
that back to the original pointer type. Casting that void* to a
type other than the original is specifically NOT guaranteed.


The conversion itself is guaranteed explicitly in 6.3.2.3p1, dereferencing
the resulting pointer is a different story.


Erm, well, sorta. The conversion is definitely guaranteed, but
even without dereferencing it can invoke UB simply due to the
fact of conversion (provided the alignment is wrong). Still,
convert it shall, and as I was talking about the legality of his
statement and not the advisability or definedness, I think I'm
alright.

-Micah
Nov 13 '05 #9

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

Similar topics

15
by: Ying Yang | last post by:
Hi, How to I make pointers of different object types point to each other? <snippet> Node2* link2; Node1* link1; link1 = link2 //error
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
4
by: Xavier Roche | last post by:
Hi folks, I have a probably rather silly question: is casting a char array in a char* a potential source of aliasing bug ? Example: a fonction returning a buffer taken in a circular buffer ...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.