Pointers in C

What was the thing that we most afraid in the C?
Yes, that are pointers. But don’t worry! We will see it step by step.
Lets start…
Definition: A pointer is a variable whose value is the address of another variable, i.e. direct address of the memory location.
Example
int var= 50;
Where,
int mean integer
var is a variable name
and 50 is a value of variable var
When we declare the integer, it will take some memory on the disc. So var has taken memory address 1001 on the memory.
Usually the memory addressees are in the hexadecimal format but we convert it in integer then they are always a positive integers.
Hence the addresses type is unsigned int.
Lets print the variable var:
printf(“%d”, var);
output:
50Lets print the address of variable var:
printf(“%u”, &var);
output:
1001Define the pointer:
int * ptr = &var;
Where,
read like: ptr is a pointer to integer
* says ptr is a special variable that mean ptr will store the address of another variable of type int.Lets print value of pointer ptr:
printf(“%u”, ptr);
output:
1001lets print the address of pointer ptr:
printf(“%u”, &ptr);
output:
2047
Can we print the value of var using pointer ptr?
Yes. Definitely.
There is one operator that is (*) value at address operator or indirection.
Print the value at address stored by pointer ptr:
printf(“%d”, *ptr);
output:
50
What is datatype of pointers?
Unsigned int.
Example:
int *p;
char *q;
float *r;int x = 5;
int *p = &x;
char ch = ‘c’;
char *q = &ch;
float f = 4.5;
float *r = &f;
The data type of the p, q, r is unsigned int(4 bytes- on 32 bit compiler). They will store the address of some variable of type int, char, float respectively.