printf (“\n the value of k=%d”,*&k); //output=10
*&k -> *2020 -> 10
*&k -> k -> 10
The reference operator (&) gives the address of ‘k’, whereas de-reference operator (*) gives value in that address, so finally it is ‘k’ value.
To differentiate ordinary variables from pointer variable, the pointer variable should proceed by called “value at address operator”. It returns the value stored at particular address. It is also called an indirection operator (symbol *).
Pointer variables must be declared with its type in the program as ordinary variables. Without declaration of a pointer variable we cannot use in the program.
A variable can be declared as a pointer variable and it points to starting byte address of any data type.
The declaration tells the compiler 3 things about variable p
a) The asterisk (*) tells that variable p is a pointer variable.
b) P needs a memory location.
c) P points to a variable of type data type.
In the first declaration v is a variable of type integer and p is a pointer variable. v stores value, p stores address In the 2nd declaration x is a pointer variable of floating point data type.
Pointer variables are initialized by p=&v, it indicates p holds the starting address of integer variable v.
Let us assume that address of v is 1000.
This address is stored in p. Now p holds address of v, now *p gives the value stored at the address pointer by p i.e. *p=20.
1. A pointer variable can be assigned to another pointer variable, if both are pointing to the same data type.
2. A pointer variable can be assigned a NULL value.
3. A pointer variable cannot be multiplied or divided by a constant.
Example : p*3 or p/3 where p is a pointer variable
4. C allows us to add integers to or subtract integers from pointers.
Example : p1+4, p2-2, p2-p1
If both p1, p2 are pointers to same way then p2-p1 gives the number of elements between p1, p2
5. Pointer variable can be incremented or decremented p++ & p- -
The values of a variable can be released in to way
1. By using variable name
2. By using adders.
#include<stdio.h>
main()
{
int k=10;
int *p;
p=&k;
printf(“\n *p=%d”,*p);
*p=20;
printf(“\n*p=%d”,*p);
printf(“\n k=%d”, k);
}
*p=10
*p=20
K=20