why it is giving the segmentation fault.and when i print ptr then it gives value 50.and when i print &ptr it gives some long negative value.
Please explain all these three output so my doubts will be cleared.
This is attempting to point ptr to address 50. Typically, you can only point to addresses within your process' address space, otherwise you'll get a segmentation fault. Java references are actually gimped pointers, which should give you an idea of how to use C pointers for basic object access. First you need a valid object, then you assign the address of that object to your pointer. Finally, you dereference the pointer to access the object:
These two lines are the rough equivalent of
Integer p = new Integer(50);
int obj = 50;
int *p = &obj;
your code is wrong becouse pointer variable just store Address of another variable so use anoter variable for the value of 50 ok sir
pointer variables are supposed to hold the address of compatible type of variables which are already declared.
pointers supposed to indicate value at that address.
You mentioned int *ptr=50. This is meaning less.
first declare int type of variable and assign the value to it.
Then pass the address of this variable to pointer variable.
Now the value can be accessed using a or *p.