Thu Jul 12 2018
Pointer in C programming
In C, pointers are used for everything. C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So, if you want to use the C language fully it becomes necessary to learn pointers to become a perfect C programmer. You need to understand pointer at a better label. It will become helpful for you. That's why, in this article, we will highlight some important feature of the pointer that will help you use the C language easily.
Let's find out what is pointer in C?
A pointer is a variable containing the address of another variable. A variable is just a labeled place to store some data. Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at runtime. The pointer variable might be belonging to any of the data types such as int, float, char, double, short etc. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Why are pointers used so much in C?
To better understand pointers, it sometimes helps to compare a “normal variable” with a pointer. Let’s say, you create an array that can hold a maximum of twenty megabytes. When the array is declared, the twenty megabytes is claimed. Now this time you have only data for ten megabytes. A next time it could be fifteen megabytes or five megabytes. So, in this case, ten megabytes of memory is wasted, because only ten megabytes from the twenty is used. This is where pointers come in. With pointers, you can create dynamic data structures. Instead of claiming the memory up-front, the memory is allocated (from the heap) while the program is running. So the exact amount of memory is claimed and there is no waste. Even better, memory not used will be returned to the heap. Free memory can be used for other programs.
How to Use Pointers?
Pointers are so commonly used in C is rarely mentioned but is very helpful in understanding how to use them. There are a few important operations, which we will do with the help of pointers very frequently. We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
Pointers are used (in the C language) in three different ways -
To create dynamic data structures
C uses pointers to create dynamic data structures - data structures built up from blocks of memory allocated from the heap at run-time.
To pass and handle variable parameters passed to functions
C uses pointers to handle variable parameters passed to functions.
To access information stored in arrays. (Especially if you work with links)
Pointers in C provide an alternative way to access information stored in arrays. Pointer techniques are especially valuable when you work with strings. There is an intimate link between arrays and pointers in C.
The following important pointer concepts should be clear to any C programmer −
Pointer arithmetic
A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. There are four arithmetic operators that can be used on pointers: ++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the address 1000. The ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location. This operation will move the pointer to the next memory location without impacting the actual value at the memory location. If ptr points to a character whose address is 1000, then the above operation will point to the location 1001 because the next character will be available at 1001.
Pointer to Pointer
A pointer to a pointer is a form of multiple indirections or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value. A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name.
Passing pointers to functions
C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.
Return pointer from functions
We have seen in the last chapter how C programming allows to return an array from a function. Similarly, C also allows returning a pointer from a function. To do so, you would have to declare a function returning a pointer.
Remember about pointers in C
-
Normal variable stores the value whereas the pointer variable stores the address of the variable.
-
The content of the C pointer always is a whole number i.e. address.
-
Always C pointer is initialized to null, i.e. int *p = null.
-
The value of the null pointer is 0.
-
Symbol & is used to get the address of the variable.
-
The symbol * is used to get the value of the variable that the pointer is pointing to.
-
If a pointer in C is assigned to NULL, it means it is pointing to nothing.
-
Two pointers can be subtracted to know how many elements are available between these two pointers.
-
But, Pointer addition, multiplication, division are not allowed.
-
The size of any pointer is 2 byte and for the 16-bit compiler.