12. C-Pointers

C-Functions Naming Convention: Previous                                          Next: C-Arrays 

C-Pointers provide the feature to play with addresses of variables. Pointers are special variables used to hold the address of another simple variable.

Syntax:    <data type> *<variable name>;
Eg:           int * num;
This statement declares the pointer variable.
<pointer variable name> = & <normal variable>;
This statement assigns the address of normal variable in pointer variable.
Eg:           int num2;
num = &num2;
when we declare a pointer variable then the data type does not belong to pointer type rather it is related with type of variable that`s address will stored in pointer variable.
Address of (&): - the address of operator is used to access the address of variable.
Eg: when we declare a variable it occupy some space in memory and that space having its own address and that address is assigned to that particular variable that variable can be accessed by writing a variable name with address of (&) operator as prefix.
This will print the 2000. And the format specifier for address is always “%u” i.e. for unsigned int, because it doesn`t have any negative value and the address can never be negative.
Value at (*): - this operator is used to print the value of a variable that stored at a particular memory address/location. It is also called Indirection and Dereferencing operator.
Eg:           int num = 2;
printf(“Value is : %d”, *(&num));                           O/P : 2
Pointer always occupies 2 bytes in memory. It doesn`t have its own particular data type.
Eg:           Pointer with different types of printf( ) statements:

Pointer to Pointer: -
Address of a pointer variable can also be store into another pointer variable; this is Pointer t Pointer constructs. To declare pointer to pointer variable, two asterisks (*) symbol are to be placed before the variable name.
Syntax:    <data type> < 2deference operator> <pointer to pointer variable name>;
Eg:           int ** num;
                Difference between pointer and pointer to pointer through a diagram:

Eg:      float a = 20;             /* Direct Assignment */
float *q1 = 20;         /* Assignment with one indirection */
float **r1 = 20;        /* Assignment with two indirection */

Pointer Arithmetic: -
When we apply an arithmetic operation on a normal variable. For Eg. If we add 5 to a normal variable whose value is 2 then the result will become 7. Same as with the pointer variable, we can perform arithmetic operation on it but it works differently. If we add into pointer variable it will point to next memory location but the working is if we add 1 to a integer pointer variable that holds 1000 as value then it will point to next 1002 memory location rather than pointing to 1001. The resulting address depends on pointer`s type.
For Eg:

If we add 1 to int type pointer variable [value: 2002] then result would be 2004 and if we add 1 in float type pointer variable[value: 2002] then result would be 2008. The addition and subtraction on a pointer variable is based on the data type of that variable whose address is hold by that pointer variable. The C allows us to increment/decrement or to add or subtract something from pointer variable.
Eg:      ptr is pointer variable.
ptr + 4 is allowed as well as ptr ++ and ptr – is also allowed. But their working are different from simple variable`s calculation.
Calculation can be performed through following way:
Base address + size of(data type) * increment
Eg: base address is 4000, data type is int, and increment is 2
= 4000 + 2 * 2
= 4000 + 4 = 4004
Next topic is Arrays in C Programming.

C-Functions Naming Convention: Previous                                          Next: C-Arrays