Tried following code to check what happens when we convert integer pointer to a integer .
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
int a;
p = (int *)malloc(sizeof(int));
*p = 10;
a = (int)p;
printf("%d\n",*p);
printf("%d \n",a);
return 0;
}
The output is : 10
135680008
Can anyone explain, the concept related to this conversion? Any links provided related to this topic would also be helpful.
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator (&). The address-of operator (&) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number.
Declaring pointers: The three following declarations are equivalent: int *p; int* p; int * p; All three of these declare the variable p as a pointer to an int.
Now, we want to assign the void pointer to integer pointer, in order to do this, we need to apply the cast operator, i.e., (int *) to the void pointer variable. This cast operator tells the compiler which type of value void pointer is holding.
Apparently you confuse the pointer with the content of the pointer.
As an analogy to the real world, you could say that, with me pointing at a bird, you want to convert my index finger to a bird. But there is no relation between the type 'bird' and 'finger'.
Transferring that analogy to your program: you are converting the object pointing to your int
to an int
itself. Since a C pointer is implemented as 'the number of a memory cell', and since there are lots of memory cells available, it's obvious that (int)p
will result in a very big number.
Casting is a nasty thing. It's a coincidence that pointers are quite analogous to integers. If they were implemented as "the nth address of the mth memory bank", you wouldn't be asking this question because there wouldn't have been an obvious relation, and you wouldn't have been able to do this cast.
135680008
is the address in decimal (it would be 0x8165008
in hex) to which p
is pointing: the address of the memory area allocated with malloc
.
Here you're printing out the memory address of a
, but you're printing it as a signed decimal integer. It doesn't make too much sense as a format, because some high memory addresses, the exact boundary depending on your word size and compiler, will be printed as negative.
The standard is to print it as an unsigned hexadecimal padded with zeroes to 8 or 16 characters (really, this is dependent on the exact word-size again). In printf
, this would be %#08X
, so the memory address 0x243 would be printed as 0x00000243.
uint64_t PointerToInt(void* ptr){
uint64_t* u=(void*)&ptr;
return *u;
}
if you want to convert pointer to 64 bits unsigned int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With