Pointers in C
Pointer is one of the most fascinating aspects of C programming language. Mastering the clever use of pointers is a gateway to become an expert C programmer. For the beginners, it is considered to be the most difficult concept to understand but it is just a myth which will be broken as you go ahead with this tutorial. Learning pointers is fun and it lets you realize and appreciate the power of C language. Some of the operations like dynamic memory allocation ( discussed later ) is not possible without pointers. Let's start exploring pointers.What is a Pointer ?
Consider the following declaration :
This declaration reserves a space of 4 bytes (integer) in memory. That memory location is named ' x ' and a value 13 is stored there. Now, every memory location has an address. Pointers are a means to store and access the address of this memory location. In simple words, a pointer variable stores the address of another variable. Note that every address is represented by a number in hex notation.
Following program demonstrate the concept :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Address of variable 'x' is 0x7ffcf2ba2054
Address of variable 'x' is 0x7ffcf2ba2054
x = 34
x = 34
x = 34
----------------------------------------------------------------------------------------------------------------------------
In the above program, we declared a pointer variable ( ptr ) of integer type. This indicates that this variable can store the address of an integer variable only. Next we store the address of another variable ( x ) in the pointer variable ( ptr ) using ampersand ( & ) operator. Next set of statements show different ways of knowing the address and value of variable x.
Pointer to a Pointer
A pointer variable stores address of another variable. It can even store the address of a pointer variable. This is where the concept of pointer to pointer comes into play.
Following program demonstrates the concept :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Address of 'x' is 0x7ffc7f0df81c
Address of 'x' is 0x7ffc7f0df81c
Address of 'i' is 0x7ffc7f0df820
x = 17.542316
x = 17.542316
x = 17.542316
----------------------------------------------------------------------------------------------------------------------------
We just need to remember that ' & ' denotes address of a variable and ' * ' denotes value at a particular address.
A statement like int ***k is also legal. It simple means it stores the address of pointer to a pointer variable. From the above program, j = &i. Similarly, k = &j.
void type Pointer
An integer pointer (int *ptr) can store an address of integer type variable (int x) only. Similarly, a floating point ptr (float *ptr) can store an address of float variable only (float x). This is valid for all data types except void which can store address of a variable of any data type. Following program illustrates the use of void type pointer :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Address of 'x' : 0x7ffd665bc034 and Value of 'x' : 27
Address of 'x' : 0x7ffd665bc033 and Value of 'x' : J
----------------------------------------------------------------------------------------------------------------------------
NULL Pointer
A pointer variable can be assigned a value NULL indicating that the variable doesn't point to any memory location. For e.g, int *ptr = NULL; The statement printf(" %x ", ptr); would print 0. So, ptr is a NULL Pointer.
Pointer Arithmetic
A pointer variable stores an address which is just a numeric value. Thus we can perform airthmetic operations on it similar to the way we do on normal variables. There are four arithmetic operators that can be used on pointers ++, --, +, - .
Following program demonstrate pointer arithmetic operations :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
ptr : 0x7fffed7fd2f4
After Incrementing :-
ptr : 0x7fffed7fd2f8
After Decrementing :-
ptr : 0x7fffed7fd2f0
----------------------------------------------------------------------------------------------------------------------------
Suppose x is stored in memory location k i.e value stored in x is k. If we do x++, x now points to (k + 4) since at location k, an integer type variable is stored whose size is 4 bytes.
If you haven't realized yet, you have already learnt the basics of pointers. Now it's time to apply the fundamentals to solve problems. Well, pointer application will be introduced as and when required in the tutorial. We will end this topic by introducing one famous application of pointers.
Function Pointers
We have seen that pointer variable points to a memory location of another variable. A function pointer points to the memory location of an executable code i.e it can store the address of a function and can be used to invoke the function it points to. Using function pointers, we can select which function to invoke at run time depending on some values. In usual technique, functions to be invoked at particular portion of the program is decided at compile time. Following program demonstrates the use of function pointers :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Sum : 7
Difference : 3
----------------------------------------------------------------------------------------------------------------------------
We can observe that at run time, either the function add ( int, int ) or subtract ( int, int ) can de selected using the function pointer ( *operate )( int, int ). Also the statement operate = &add; can be written as
operate = add; i.e ' & ' is optional here.
No comments:
Post a Comment