HowToProgramC : Lesson 19


Back Home Next



Pointers

In the earlier lessons, we have briefly referred to the concept of pointers. Let’s see what a pointer is and how it can be useful. Pointers are a special type of variables in which a memory address is stored. They contain a memory address, not the value of the variable. The concept of the pointers can be well understood from the following example.

Suppose, hundreds of people are sitting in an auditorium. The host is going to announce a prize for
a person amongst the audience. There are two methods to call the prizewinner to stage. The host can either call the name of the person or the number of the seat. These are equivalent to ‘call by name’ and ‘call by address’ methods. In both cases, the prize will be delivered to a person whether he is called by name or referred by address (seat number in this case). In programming, pointers are used to refer by the addresses.

Declaration of Pointers

Pointers work by pointing to a particular data type. We can have pointer to an integer, pointer to a double, pointer to a character and so on. It means that a type is associated to a pointer. Pointer, being a variable, needs a name. The rules for naming a pointer are the same as for the simple variable names. The pointers are declared in a specific way. The syntax of declaring a pointer is:

data type *name ;

Here ‘name’ is the name of the pointer and data type is the type of the data to which the pointer (name) points. There is no space between asterisk (*) and the name. Each variable being declared as a pointer must be preceded by *. The * is associated with the name of the variable, not with the data type. To associate the * (asterisk) with data type (like int* ) may confuse the declaration statement. Suppose, we want to declare a pointer to an integer. We will write as:

int *myptr;

Here myptr is the name of the pointer. The easiest way to understand the pointer declaration line is the reading the statement from right to left. For the above statement, we say that myptr is a pointer to an integer (int). Similarly for the declaration double *x , x is a pointer to a data of type double. The declaration of char *c shows that c is a pointer to a data of type character. The declaration of multiple pointers requires the use of * with each variable name. This is evident from the following example which declares three pointers.

int *ptr1, *ptr2, *ptr3 ;

Moreover, we can mix the pointers declaration with simple variables on one line.

int *ptr, x, a [10] ;

In this declaration ptr is a pointer to data of type int, x is a simple variable of type int and a is an array of integers. Whenever used, these pointers hold memory addresses. Now we will try to understand what address a pointer holds. Suppose, we declare a pointer variable ptr and a variable x and assign a value 10 to it. We write this as under.

int *ptr ;
int x ;
x = 10 ;

Here x is a name of a memory location where a value 10 is stored. We want to store the address of this memory location (which is labeled as x) into the pointer ptr. To get the address of x, we use address operator i.e. &. (it is & not &&, the && is logical AND). To assign the address of x to pointer ptr, we write

ptr = &x ;

This statement assigns the memory address of the location x to the pointer ptr.In the above assignment statement, we have a pointer to a memory location. Now, it can be ascertained what value is stored in that memory location. To get the value stored at a memory address, we use the dereferencing operator, represented by asterisk (*). The * is used with the name of the pointer to get the value stored at that address. To get the value stored at the memory address ptr, we write *ptr which is read as the value of whatever ptr points to. Thus the line z = *ptr; means, z has the value of whatever ptr points to. We can use this operator (*) to get the value and can do any arithmetic operation with it. The following statements make it further clear.

z = *ptr + 2 ;
z = *ptr * 2 ;
z = *ptr – 2 ;

Here *ptr gives the value stored at memory address where the pointer ptr points to.



Back Home Next


Google




learntoknow@yahoo.com
© All rights Reserved.