473,322 Members | 1,188 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,322 software developers and data experts.

Pointer conversions and Data types conversions

vb
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?

Nov 15 '05 #1
10 2183
vb@gmail.com wrote:

Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?


No.
A pointer can be converted to a pointer to void,
and converted back to the original type and value.
Little else is guaranteed about conversions to pointer to void.

--
pete
Nov 15 '05 #2

vb@gmail.com wrote:
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?

Every pointer is an address of memory.Most of the pointers are integers
and a few are not (This has been mentioned in comp.lang.c.).The kind of
pointer will tell the compiler how many bytes and what type of the data
it points to.So we can cast a pointer from one type to another,but it
is not safe and maybe you get some warnings!You should be careful
enough to use a 'void*' pointer,though it is very useful.

Nov 15 '05 #3
vb@gmail.com wrote:
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer
Casts are explicit conversions, and actually a side-issue here. What you are
really talking about is conversions in general, not necessarily explicit
conversions.

In fact, the guarantee in the Standard amounts to this: that, for any object
type T, the following transformation:

T *tp = &SomeObjectOrOtherOfTypeT;
void *vp = tp;
T *ntp = vp;

does not lose information. That is, after this code is executed, tp and ntp
will point to the same object.

If you are silly enough to do this:

T *tp = &SomeTOrOther;
void *vp = tp;
U *up = vp;

then you are skating on much thinner ice. For example, pointers-to-T and
pointers-to-U might have different numbers of significant bits.

so this implies that any pointer can be
cast to any other pointer. Is that so?


Yes, there is no limit to the thinness of the ice on which you can skate.
Sooner or later, though, the surface will crack, and in you'll go. Wise men
observe the signs, rather than take the dunking.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #4
Cong Wang wrote:

So we can cast a pointer from one type to another,but it
is not safe and maybe you get some warnings!


Unfortunately, the cast is more likely to /remove/ some warnings, which is
why many people are silly enough to use it so often.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #5
pete <pf*****@mindspring.com> wrote:
vb@gmail.com wrote:

I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?


No.
A pointer can be converted to a pointer to void,
and converted back to the original type and value.
Little else is guaranteed about conversions to pointer to void.


This is true for object pointers (and pointers to incomplete types), not
for function pointers.

Pointers to types not qualified const etc. may be converted to pointers
to that same type with such qualifiers, and the result is a usable
pointer to the same object or function as the original.
Null pointer constants may be converted to any pointer type, and will
result in a null pointer; and null pointers may be converted to any
other kind of null pointer.
Integers may be converted to pointers, but not usefully unless your
implementation specifies this beyond the Standard (some do). Ditto vice
versa, and if the result can't be represented in the target type, you
have outright UB.
Object and incomplete-type pointers may be converted to one another, but
not usefully (the result is not required to be well-aligned, and if not,
the result is UB), except for:
Object pointers may be converted to pointer to (any kind of) char, and
the result points at the first byte of the object pointed to.
Function pointers may be converted to any function pointer type, and
remain their original value; you cannot reliably call a function through
a pointer of the wrong type, but you can convert it back to the right
type and get a callable pointer.

Richard
Nov 15 '05 #6
Never do such things in real code. you will do a segfault in 90% of
cases.

Nov 15 '05 #7
On Mon, 01 Aug 2005 01:02:22 -0700, Cong Wang wrote:

vb@gmail.com wrote:
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so? Every pointer is an address of memory.


Not necessarily. For example a null pointer need not in any sense
correspond to a memory address.
Most of the pointers are integers
and a few are not (This has been mentioned in comp.lang.c.).
There is no sense in which C considers a pointer to be an integer. C does
allow casting between pointer and integer types but the language doesn't
define the result in most cases, except that a null pointer constant
(which may be an integer) converted to a pointer type gives a null
pointer. Thinking of pointers as integers isn't really useful and can
lead to dangerous/wrong assumptions.
The kind of
pointer will tell the compiler how many bytes and what type of the data
it points to.
A pointer to object type indicates the size of object being pointed at, a
pointer to incomplete type (e.g. void *, pointer to an incomplete
structure, union or array typeor a pointer to a function doesn't.
So we can cast a pointer from one type to another,but it
is not safe and maybe you get some warnings!You should be careful
enough to use a 'void*' pointer,though it is very useful.


Casts very often get rid of warnings which is what makes them so
dangerous. Just because the compiler doesn't warn doesn't mean the code is
correct or even well defined.

Lawrence
Nov 15 '05 #8
In article <11**********************@o13g2000cwo.googlegroups .com>
vb@gmail.com <vb*****@gmail.com> wrote:
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?


As others have noted, it is not so. The reasoning here has a
flaw.

You are correct in that any "data pointer" (but not function pointer)
can be converted to "void *" and back without losing any important
information. But from this, you conclude that any (data) pointer
can be converted to any other data pointer. Why?

Consider, if you will, "int" and "double" as a similar example.
Suppose that "int" ranges from -32768 to +32767 (as it does on most
16-bit CPUs) or even -2147483648 to +2147483647 (as it does on most
32-bit CPUs), while "double" is a typical 8-byte IEEE double that
has 53 "mantissa" bits and thus can represent all integers up to
+/- 9007199254740992 (remember that IEEE floating point uses
sign-and-magnitude representation, so unlike two's complement,
the range is symmetric).

Now imagine that "double" is analagous to both "void *" and
"char *" ("byte pointers"), while "int" is the analagous to
"int *" and other "word pointers". You can always take any
word pointer and store it in a byte pointer, just as you can
always take an ordinary "int" value and store it in a "double".
But there are "double" values that you cannot store in an
"int", such as 3.5. If you store 3.5 in an int, the 0.5
part "falls off the end", and when you convert it back to
double, you get 3.0.

The same actually happens (on some real machines) when you use byte
pointers and word pointers. Word pointers only ever point to "whole
words": word 0, word 1, word 2, and so on. But each "whole word"
is made up of at least 2 (and as many as 8, on the Cray) "bytes"
-- so a byte pointer needs one, two, or even three more bits than
a word pointer. When you convert from one to the other, the extra
bits are added or removed as needed. If the bits in a byte pointer
were not zero, removing those bits discards "useful information",
and when you convert the word pointer back to a byte pointer, the
byte offset within the word is gone: the pointer's value has changed.

Less concretely (but perhaps easier to remember): Think of pointer
values as water, and pointer objects (of various types, like
"int *" and "struct foo *") as cups, glasses, mugs, beer-steins,
and the like. "void *" is a Really Big Bucket, into which you can
pour *any* container of pointer. You can then pour the contents
back into the original container, because the Big Bucket will only
be as full as the original container was. But if you pour a beer
stein ("char *") into the bucket ("void *"), then pour the bucket
into a shot-glass ("int *"), some of the beer will slop out.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #9
as*********@gmail.com writes:
Never do such things in real code. you will do a segfault in 90% of
cases.


Context, dammit!

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
In article <11**********************@o13g2000cwo.googlegroups .com>, vb@gmail.com wrote:
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to me, since any pointer can be cast to void* and void * can
be cast to any other pointer so this implies that any pointer can be
cast to any other pointer. Is that so?


You can do the cast, but there's no guarantee that the converted pointer will
work unless it originated from the same type.

S if you start with int* and convert it to char* or void* and then back to
int* that's legal, but if you start with char* and convert it to int*
and try to use it some hardwarw will baulk unless it's word aligned
(for whatever word width the architecture uses)
Bye.
Jasen
Nov 15 '05 #11

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

Similar topics

35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
23
by: Leon Brodskiy | last post by:
Hi, Could please anyone clarify about pointer and array in C? If I have: int arr; The following two commands will be the same: arr and &arr.
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
11
by: quakewang | last post by:
hi, I have define in a head file like this: #define GLUT_BITMAP_9_BY_15 ((void*)2) #define GLUT_BITMAP_8_BY_13 ((void*)3) #define GLUT_BITMAP_TIMES_ROMAN_10 ...
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...
12
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.