One of the interesting features that most C programmers enjoy and hates is pointers. Some C programmers enjoy it because it gives immense control over operations in programming. Reason for hating it is due to the fact that it can cause massive errors in program. If you can learn to handle this wisely, you will be a really good C programmer.
Pointers in C
Pointers are often used to make C programming much easier and it helps programmers improve their program's efficiency. Not only that, by using the pointer's you can achieve unlimited amount of memory usage in your program, that’s cool right.
Definitions and Uses of Pointers
Pointers are the variables that hold an address of another variable of the same data type. The cool thing about the address of a variable is that, you'll then be able to go to that address and access the data stored in it. Pointers are basically used in the C in different ways. Some of them are:
- To create dynamic and flexible data structures.
- To pass and handle variable arguments passed to functions (Call by reference).
- To retrieve information stored in list like arrays.
As I said earlier, pointers indicate the memory address of any kind of object i.e. It can be address of any primitive data types or structures or even functions. The variable which holds the address is called the pointer variable.
Typical Pointer Declarations
Let’s see how we can declare a pointer in C. Generally say integer variable “var” is declared like below.
In case of pointer for example, say integer type of pointer the declaration is like this:
In general the declaration is like
<type> *<variable name>;
That’s why it is recommended that you should always initialize a pointer value with NULL like this.
int *var = NULL;
You have noticed that "*" operator is used at declaration to denote a variable to a pointer type. Another operator "&" is used to represent the address of a variable.
int i = 10;
int *p = &i;
When you declare a variable, a memory is allocated for that variable. For the above example, say the variable "i" has address 0x2000 and the address contains value 10, now when the "p" variable is declared, it also gets a memory address say 0x2100, because we have declared that it will contain address of I, so it will contain the value of 0x2000.
0x2000 ( i ) 0x2100 ( p )
As you seen earlier, "*" operator is used to declare the pointer type of the variable. "*" is also used to dereference the pointer also.
int j = *p;
It sets the value of "j" variable to the value contained at the memory address, the pointer p points to i.e. 10. You can also set a value to the memory address that a pointer holds like below.
*p = 20;
By this, the memory address that "p" pointer holds i.e. 0x2000 contains value 20 which is same as the value of i and *j, interesting right! That’s the beauty of the pointer, as you change the value that contains an address of a variable; it also changes the variable itself.
You must have noticed that a pointer always holds an address of the same data type, like an integer, pointer always holds the address of integer type of variable, float of a float, double of a double etc.
But there is a special type of pointer called void pointer also known as generic pointer. This type of pointer can hold the memory address of any kind of data type or any object even functions too! This is declared like below:
void *void_ptr = &i;
But the only thing you need to remember is that you can’t dereference a pointer directly. You have to typecast it before you use it. Like if you want to print the value of the above declared void pointer, the memory address it contains point to is:
This is very useful when you want a pointer to point to data of different data types at different times.
You are well aware about arithmetic of other variable like int, float, double etc. and you must think about whether those can be applied to a pointer variable or not. No, you can’t apply arithmetic operation like addition, subtraction, multiplications etc. between two pointer variables. The operations given below give you a Compiler Error.
int *pi = &i, *pj = &j;
int *res = *pi + *pj /* Compilation Error */
res = *pi - *pj /* Compilation Error */
res = *pi * *pj /* Compilation Error */
Only thing you can do is to increment and decrement operation to a pointer variable. This is mostly used by C programmers when dealing with arrays or strings.
char s = "a4academics.com";
char* ptr = s;
printf(“%c\n”,*(ptr + 3));
The above gives following output: