how do you read this pointer?
Code:*(char *)ptr
how do you read this pointer?
Code:*(char *)ptr
the value at the location pointed to by ptr when ptr is interpeted as a pointer to a character.
In a long winded way of course
Basically (I'm pretty sure) it would be the same as this:
*((char *)ptr)
It converts ptr into a char * and is the value at that converted pointer.
Do not make direct eye contact with me.
I wouldn't use the world convert. Casting is a temporary change in interpretation while conversion implies some level of permanence.
Edit: Short winded version:
treat ptr as a pointer to a char and retrieve the value at the location pointed to.
One of the few situations where one may convert a pointer of one type to another type and then get the value I could think of is
This will work for only for the first member of a structure. Similar things mighr be done for a union too. Another example I can think of is casting the pointer to an object to an unsigned char * and then looking at the bytes that make up the objectCode:#include <stdlib.h> typedef struct { double d; char c; } bstruct; int main(void) { bstruct *b; double db; b=malloc(sizeof *b); b->d=1.2; b->c='a'; /*some programming statements later*/ db= *((double *) b); printf("%f\n",db); /*some more statements*/ free(b); return 0; }
Any other examples?Code:#include <stdio.h> #include <stdlib.h> int main(void) { double d=1.5; unsigned char *pc; size_t i; pc=(unsigned char *)&d; for(i=0;i<sizeof d;i++,pc++) printf("%X ",(unsigned) *pc); return 0; }
Last edited by pinko_liberal; 05-26-2004 at 01:16 PM.
The one who says it cannot be done should never interrupt the one who is doing it.
@pinko: Do you mean something like this?The output won't be 5 I can tell you thatCode:float x = 5.6f; printf("%d\n", *(int *)&x);
@Dave: I read the link and understand what you are trying to say. Now if you go back and re-read my post I think you'll see why I favor against using the word convert, especially for pointers.
Look at the example I just gave. Did x change value? No. Did the bit pattern change? Nope. Only thing that changed is how we are interpeting the bit pattern.
I'll admit that there are some cases where casting is more like convertion and in those cases it might be the better term. This isn't one of those cases.
On your implementation, perhaps there was no noticeable change. But this is not necessarily always the case. Different objects can have different pointers that do indeed have different representations. For example, a char* could have a different representation than an int*. Program for the DS9000, not a friendly PC.Originally Posted by Thantos
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
You care to prove your statement with something other then a vain attempt at humor?
Spend some time Googling comp.lang.c: here's a start.
While there, you may discover that the DS9000 is a reference to an imaginary machine that implements the C standard, but undefined behavior would always result in disaster: a most rigorous test environment for C code where faulty assumptions die a cruel death.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
On an imaginary machinecould result in an error. Show me something in reality and I might care.Code:int x = 0;
This is the last time I do the Googling for you.
http://groups.google.com/groups?hl=e...3DN%26tab%3Dwg
[edit]And this explains why I'm feeling deja vu.
Last edited by Dave_Sinkula; 05-26-2004 at 04:14 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
[QUOTE=Thantos]@pinko: Do you mean something like this?[QUOTE]Code:float x = 5.6f; printf("%d\n", *(int *)&x);
I was wondering what kind of pointer conversions are compatible ,other than conversions to void *
The one who says it cannot be done should never interrupt the one who is doing it.