There was a need for a kind of variable that could store the address of another variable, so that the value of he variable could be directly accessed through its memory address and could be manipulated more easily and in a short span of time. The “pointer” is such a variable invented. It can be defined as a variable which stores the address of another variable. A pointer can be declared easily as shown below:
int *ptr1;
The “int” keyword is used to tell compiler that the pointer “ptr1” will store the memory address of an integer type variable. The symbol “*” called asterisk is used to tell the compiler that the variable “ptr1” is actually a pointer and that it will store the address of the variable it is pointing to irrespective of the bytes it will require to store the memory address. The pointer “ptr1” is said to point to an integer variable. N the above declaration we didn’t provide ptr1 with a value i.e., it’s empty now. If the declaration is made outside the function, the pointer “ptr1” will be initialized to a value that will not point to any of the variables or objects in a C program. Pointers initialized in this manner are said to have a null value or are called as “null” pointers. Null pointers are very useful in many of the C programs as they prevent system crash. A null pointer is implemented using a macro. The macro used is called “NULL” macro. If you set the value of a pointer using the above mentioned macro through an assignment statement as shown below:
Ptr1 = NULL;
It is assured that the pointer is now having a null value or it has become a null pointer. A null pointer can be tested using the below given statement
if (ptr 1== NULL);
Now suppose we want to store the address of an integer a in the above declared pointer “ptr1”, we will use the following statement:
ptr1 = &a;
Before proceeding further, we should now that a pointer has two values attached to it. One is the “l value” and the other one is the “r value”. L value is where the address of the variable pointed to is stored. R value stores the value of that variable. Now, the function of the “&” operator is to retrieve this l value of the variable a. the assignment operator copies the address of the variable a to the pointer “ptr1”. Now the pointer “ptr1” is said to point to variable “a”.
The “*” operator is also called the dereferencing operator and is used for de-referencing as follows:
*ptr 1 = 10;
The above statement will copy the value “10” to the address of variable a pointed to by the pointer “ptr1”. The above assignment statement can be written in another way as shown below:
Printf ( “ % d \n ”, *ptr1 );
The above statement will also print the value stored in the pointer on the screen as output.
Pointers are very much essential nowadays in programs. They solve basically two problems avoiding many other problems that may follow. First problem that they solve is that they make it easy to share information and data through and from the different sections of the memory. Secondly, they solve the problem of having complex structures. They make it easy to have linked data structures namely linked lists and queues and also binary trees. Pointers reduce the complexity of the program and there are many things that one can do using only pointers. Pointers are by no doubt a powerful C construct.
Thursday, October 6, 2011
Some details about Pointers in C...
Posted by Sunflower at 10/06/2011 06:17:00 PM
Labels: C, C Language, Compiler, Complex, Complexity, Data, Functions, Memory, Objects, Pointers, Points, Programs, Storage, Structures, Values, Variables
Subscribe by Email |
|
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment