Your preference for decimal is showing.Quote:
I used %u so it's easy to manipulate.
The short answer: there is a format %p that you are supposed to use to print an address. Consequently, no other format needs to be implemented so that it works.Quote:
Can u please explain the meaning of "undefined behaviour"?
Longer answer: One thing that makes %u wrong is that you are assuming the unsigned integer type and the pointer type will always be the same size. You are also assuming that pointer types are all the same size. Many implementations are accommodating because the assumptions you make are true on your home computer, running 32-bit code. Neither of these things are guaranteed to be true so your results will be suspect on platforms different from your home computer. Your choice of integer format is also completely arbitrary. Nothing makes %u more suitable than %d for replacing the proper choice, %p.
You are not even printing what the comments say you are printing. You are printing the addresses of the location of the pointers themselves instead of the pointers' values. Printing the pointers' values looks like this:Quote:
1. This printf statement will print the address of char 'T' of string "This" and variable 'charr' will be stored in symbol table to link it when variable is used.
2. In this when chptr will be used it will bind the address where address of string "this" is stored. and chptr will take 4 byte to store the starting address of "this".
There are also things that make no sense whatsoever in your comments. What is being passed to printf is a pointer char* cast to void* so that will take up stack space in the printf function. The initial values of the array charr, and the literal string used to initialize chptr are also stored somewhere, so that will take up space. And finally, gcc -- along with any compiler -- does not allocate memory for any program. The OS is in charge of that, when the program is executing.Code:#include <stdio.h>
int main(void)
{
char charr[]="This";
char *chptr="This";
printf("%p\n", (void*)charr);
printf("%p\n", (void*)chptr);
return 0;
}