-1

What happens when a pointer is typecasted to a basic data type. Why do we get some value?

For example:

int h=4;
int * ph=&h;
printf("%p",ph);
printf("%d",ph);

Both the print statements print different values...

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    `Int` ? Also I am unable to see any casting . What you do is just integer pointer (`int *`) in which address of a integer variable (`int`) is stored. And don't print pointer value with `%d` . – ameyCU Mar 08 '16 at 15:38
  • Print the pointer using `%p` also - `printf("%p\n", p);`. Then compare the results. – R Sahu Mar 08 '16 at 15:43
  • when you want print pointer it shoudl be casted to `void*` – whd Mar 08 '16 at 15:43
  • I tried running that and i get `0xbfcd8e34` and `-1077047756` which is the same value. What result are you getting? – Alderath Mar 08 '16 at 15:45
  • @ameyCU Actually there is implicit casting in the second print statement...because the format specifier is of type integer..it'll implicitly cast the value of pointer to int. The doubt was I dint know how did it get that value... Cleared now..thanks.. – Sumanth K gowda Mar 09 '16 at 14:00
  • @RSahu yeah I have done that... It's mentioned in the code.. – Sumanth K gowda Mar 09 '16 at 14:01
  • @whd What do u mean it should be casted to void*...can you be a bit more clear about what is void* please.? – Sumanth K gowda Mar 09 '16 at 14:02
  • @Alderath Yeah something similar to that... – Sumanth K gowda Mar 09 '16 at 14:02
  • @SumanthKgowda Specifier `%p` expects type to be `(void *)` , therefore you need to explicitly cast it to `(void *)` . – ameyCU Mar 09 '16 at 14:26
  • @ameyCU Sorry if it's silly....what exactly is void *.? – Sumanth K gowda Mar 09 '16 at 14:30
  • @SumanthKgowda An example - `void *p;` , so `p` is a void pointer and can be typecasted to or from any type. Here you typecast the `int *` to `void *` using cast `(void *)` so as to pass argument to `%p` . – ameyCU Mar 09 '16 at 14:32
  • @ameyCU Oh ya got it thank you......but doesn't it get implicitly typecasted in printf statement...we won't do it explicitly right..? – Sumanth K gowda Mar 09 '16 at 14:36
  • @SumanthKgowda No ,you need to do that `printf` won't do that .You can refer here if you want to read in detail - http://stackoverflow.com/questions/8303673/why-cast-is-needed-in-printf – ameyCU Mar 09 '16 at 14:38
  • @ameyCU No ..,I just tried it out in both the ways.. It gave the same outputs...so it doesn't matter...it actaully typecasts it implicitly.. – Sumanth K gowda Mar 09 '16 at 14:44
  • @ameyCU actually the answer is in the word void* itself..it does mean that it can be typecasted from or to any type...so basically it says that it can be of any type unless it's a pointer..so u need not have to explicitly typecaste it while printing..it accepts all type of pointers...but if u r printing any other variables with different format specifiers w.r.t variables in the arguments of printf statement..then u have to explicitly typecaste it...if not u may get garbage or some value.. And thanks that link was useful.. – Sumanth K gowda Mar 09 '16 at 15:10

2 Answers2

0
printf("%p",ph);

says to the runtime "See over there, that memory is a pointer, load it and print it out as hex please". Note that this is not said to the compiler, the compiler doesnt know what printf is doing (actually most modern compilers sneak a look inside printf statements, you probably got a warning).

printf("%d",ph);

says - "See that piece of memory, its an integer, please load it and print it as a human readable base 10 number"

Given that ph is a pointer to an int the first one does the correct thing, it prints out the value of the pointer.

The second one's behavior depends on the size and representation of int and pointers on your system. The value is 'really' a pointer but you are telling the runtime its an int. ON many many systems pointers and ints are 32 bits. In that case the load will load 32 bits and the print will interpret those bits as an int and print out the base 10 value of the pointer. On other systems pointers might be 64 bits and int would still be 32 bit. Since you dont say what value you get out its hard to know whats going on but if I had to guess I would say that you are getting the same value, one in hex the other in decimal

Note that the second one is whats called 'undefined behavior', you are lying to the system: bad, confusing, unexplained things can happen

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Thank you so much..I almost got it...basically what u said was both the values represents the address only but in different format specified by the format specifier.....did I get it right..? – Sumanth K gowda Mar 09 '16 at 14:05
  • no. The format specifier tells printf how to treat the memory. The memory content of ph is alwasy the same – pm100 Mar 09 '16 at 19:18
-1

This program has undefined behaviour. The type of actual paramater ph (int*) doesn't match format specifier "%d". It can print anything or nothing and simply make your PC explode.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • And what is the problem with my answer? – SergeyA Mar 08 '16 at 17:22
  • No dude it's not that it prints anything or nothing...it actually prints the same address value in the integer format....so your PC may not explode ;)..and thank you for your answer ..it was useful.. – Sumanth K gowda Mar 09 '16 at 14:24
  • @SumanthKgowda That was a phrase in the answer, but second statement is UB therefore getting correct answer is a possibility but _don't rely on that_ . – ameyCU Mar 09 '16 at 14:27
  • @ameyCU Ohh ok...but I got the same int value every time for the same address...does that mean it depends on compiler version or something..? – Sumanth K gowda Mar 09 '16 at 14:33
  • @SumanthKgowda You may get _same or different_ result on different machines . – ameyCU Mar 09 '16 at 14:34