Previous: 6.1 What is a Pointer?
Up: 6.1 What is a Pointer?
Next: 6.1.2 Indirect Access of Values
Previous Page: 6.1 What is a Pointer?
Next Page: 6.1.2 Indirect Access of Values
Before we discuss passing pointers and indirectly accessing data between functions, let us look at how we can declare pointer variables and access data using them. Consider the following simple program:
main()
{ int x;
int iptr;
printf("***Testing Pointer Variables***\n");
x = 10;
iptr = &x;
printf("%d\n",iptr);
}
We have declared two integers, x, intended to hold an integer value,
and iptr which is intended to hold a pointer to an integer, i.e.
and address of an integer.
We then assign a value to x, and the address of x to the
variable iptr using the & (address of) operator.
The address of a variable is simply the byte address of the cell which
was allocated by the declaration. An address is an integer (actually and
unsigned integer) so may be stored in an int type variable.
The situation is shown in Figure 6.1a).
When we compile and execute this program the result is:
What if we had wanted to print the value of the cell pointed to by iptr and not the value of iptr itself? The indirection operator, *, accesses the object pointed to by its operand. In our example, the value of iptr is 1000 which is an address of some object; i.e. iptr points to some object located at address 1000. So we should be able to access that object with an expression like:
*iptrHowever, there is no way to know how many bytes to access at address 1000, nor how to interpret the data, unless the type of object at address 1000 is known: is it an int? a float? a char? etc. In order for the compiler to know how to access an object indirectly, it must know the type of that object. We specify the type of object to access by indicating to the compiler the type of objects a pointer refers to when we declare the pointer. So, in our example, we should declare the variable, iptr as a ``pointer to an integer'' as follows:
int *iptr;or,
int * iptr;(white space may separate the operator, *, and the variable name, iptr). The declaration specifies a variable, iptr, of type int *, i.e. integer pointer (the type is read directly from the declaration). So, int * is the type of iptr, and int is the type of *iptr --- the thing it points to. This statement declares an integer pointer variable, iptr, and allocates memory for a pointer variable. Similarly, we can declare float pointers or character pointers:
float * pa, * pb;
char * pc;
These statements declare variables, pa and pb, which can point to
float type objects,
and pc which can point to a char type object.
All pointer variables store
addresses, which are unsigned integers, and so need the same amount of memory
space regardless of the pointer types.
Since the compiler now knows that iptr points to an integer object, it can access the object correctly. Our simple program becomes:
main()
{ int x;
int *iptr;
printf("***Testing Pointer Variables***\n");
x = 10;
iptr = &x;
printf("Address %d holds value %d\n",iptr,*iptr);
}
which produces the output:
In summary, the address of an object is called a pointer to that object since the address tells one where to go in order to access the object. The address by itself does not provide sufficient information to access an object; we must know what type of object the address is pointing to. If the pointer (address) value and the data type of the object that it points to are both known, then it is possible to access the object correctly. In other words, pointers must be specified to be int pointers, pointing to an integer type object, float pointers, pointing to a floating point type object, char pointers, etc.