Monday 9 March 2015

Pointers in C


Pointers are another important feature of the C language. Although it may appear a little confusing for a novice programmer they are a powerful tool and handy to use once they are mastered. The power of C compared to most other languages lies with proper use of pointers. Pointers are useful due to following reasons:

· They enable us to access a variable that is defined outside a function.
· Pointers are more efficient in handling data tables and sometimes even arrays.
· Pointers tend to reduce the length and complexity of a program.
· They increase the execution speed.
· Use of pointers allows easy access to character strings.

Computers use memory to store both instructions and values of variables of a program. The computer’s memory is a sequential collection of storage cells with the capacity of a single byte. Each of these memory cells has an address associated with it.

Whenever a variable is declared the system allocates some memory to hold the value of the variable. Such memory location can be accessed by providing the memory address. Consider the following example:

int number = 35;

The above expression allocates a memory location to hold the value of variable “number” that can hold an integer (4 bytes) and it also initialises the variable. Suppose the address of that memory location is 2000. Then after executing above expression the memory address 2000 should hold 35. During execution of the program, the system always associates the name “number” with the memory address 2000. We may have access to the value “35” by using either the name “number” or the address 2000. Since memory addresses are simple numbers, they can also be assigned to some variables. Such variables that hold memory addresses are called pointers. Therefore a pointer is nothing but a variable that contains an address which is a location of another variable in memory.

Declaring Pointers

A pointer is declared using the indirection (*) operator. The typical declaration of a pointer is:

data-type *pointer-name;

If a pointer “a” is pointing to an integer, it is declared as:

int *a;

Since a pointer is a variable, its value is also stored in another memory location. Therefore in computation even the address of the pointer can be used.

The location of a variable in memory is system dependent and therefore the address of a variable is not known directly. The address operator (&) allow us to retrieve the address from a variable associated with it. Consider the following example:

/* Program-8.1 */

#include <stdio.h>
int main()
{
int number = 20;
int *pnt;
pnt = &number;
printf("\nThe number is: %d", number);
printf("\nThe address of the number is: %d", &number);
printf("\nThe pointer is: %d", pnt);
printf("\nThe address of the pointer is: %d", &pnt);
printf("\nThe value of the pointer is: %d", *pnt);
return 0;
}
Execution of Program-8.1 displays:

The number is: 20
The address of the number is: 1245064
The pointer is: 1245064
The address of the pointer is: 1245060
The value of the pointer is: 20

The first printf function displays the value of variable “number”. The second printf statement displays the address of the memory location occupied by the variable named “number”. The third statement displays the value of the “pnt” which is assigned by the expression pnt = &number;. Note that now the address of variable “number” and value of pointer “pnt” is the same. The fourth printf function displays the address of the pointer. The final statement displays the value of the pointer “pnt” which holds the value of the variable “number”.

Example 8.1 – Write a program to swap two integer numbers using pointers.

/* Program-8.2 */

#include <stdio.h>

void swap(int *a,int *b);

int main()
{
int a,b;
a = 5;
b = 10;
printf("\nBefore swapping a= %d: b= %d", a, b);
swap(&a, &b); //call function
printf("\nAfter swapping a= %d: b= %d", a, b);
return 0;
}
void swap(int *a, int *b)
{
int x;
x = *b;
*b = *a;
*a = x;
}

Execution of Program-8.2 displays:

Before swapping a= 5: b= 10
After swapping a= 10: b= 5


Text Strings and Pointers

An array of characters is called a string. Strings in C are handled differently than most other languages. A pointer is used to keep track of a text string stored in memory. It will point to the first character of the string. By knowing the beginning address and the length of the string, the program can locate it.

A character pointer is used to point to the first character of a string as given in the following example:

char *a;
a = "Hello World!";

Consider the following example:


#include <stdio.h>
int main()
{
char *a;
a = "Hello World";
printf("String: %s\n", a);
printf("First character: %c\n", *a);
printf("Starting memory address: %d\n", a);
printf("First character: %d\n", *a);
return 0;
}

Exaction of Program-8.3 will display:

String: Hello World
First character: H

Starting memory address: 4235496
First character: 72

In Program-8.3 the first printf function displays the string pointed by pointer “a”. The second printf function display the value pointed by pointer “a” which is the first character of the string. The third printf function displays the starting memory address of the string which is the value of pointer “a”. The final printf function displays the ASCII value of the first character in the string.







No comments:

Post a Comment