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.
Definition: A pointer is a variable whose value is the address of another variable, i.e. direct address of the memory location.
int var= 50;
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:
Lets print the address of variable var:
Define the pointer:
int * ptr = &var;
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:
lets print the address of pointer ptr:
Can we print the value of var using pointer ptr?
There is one operator that is (*) value at address operator or indirection.
Print the value at address stored by pointer ptr:
What is datatype of pointers?
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.