By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

casting pointers

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.