Actually what your trying is out of your eagerness., I agree, I too often do this way.
The Very first thing is if you store 13232323
in a pointer variable, the hex value of it is OXC9E8C3
., so really at the time of your assigning the pointer variable (ptr) doesnot know whether really it is a valid address or invalid address. But when you dereference this address with *ptr
, then comes the problem. It tries to seek the value in the address.
Then there are 2 cases..
- Really if what you assigned is a valid address , then it will return the value. (Practically impossible case)
- Mostly your address will be a invalid one, hence program will crash (Segmentation fault).
So, even though your program compiles, runs, until and unless you store a valid address in ptr
., ptr
has no use.
Your Q: It works as it was surposed to be,except that I can not construct an
address containing characters ABCDEF,so,what's the big difference
between int and pointer? printf("%d",ptr);
I think you are asking, whatever the case, I cannot store ABCDEF, hence ptr
works same as int type
, so what is the difference between integer and pointer?
Here it is :
- You cannot deference an integer value, where as pointer can do it.
Hence it is called as pointer :)
- You are seeing only numbers because you are printing the address with
%d
, trying printing with %x
or %p
.
Atlast, do you notice the compiler warning, warning: assignment makes pointer from integer without a cast
, because ptr = j;
in that ptr
is of int*
type, and j
is of int
type.