Lecture 8 - Pointers
Lecture 8 - Pointers
Table of Contents
What Is A Pointer? ........................................................................................................................................ 1
Pointer Declaration ....................................................................................................................................... 1
Pointer Operators ......................................................................................................................................... 1
Operations Performed Using Pointers .......................................................................................................... 3
Example: Demonstrating pointers ............................................................................................................ 3
Example : Further demonstration of pointers .......................................................................................... 3
Note............................................................................................................................................................... 4
What Is A Pointer?
A pointer is a variable that holds the memory address of another variable. For example, if a
variable called p contains the address of another variable called q, then p is said to point to q.
Therefore if q were at location 100 in memory, then p would have the value 100.
Pointer Declaration
Here, type is the base type of the pointer. The base type specifies the type of the object that
the pointer can point to. Notice that an asterisk precedes the variable name. This tells the
computer that a pointer variable is being created. For example, the following statement creates
a pointer to an integer.
int *p;
Pointer Operators
C contains two special pointer operators: * and &. The & operator returns the address of the
variable it precedes. The * operator returns the value stored at the address that it precedes.
The * pointer operator has no relationship to the multiplication operator, which uses the same
symbol). For example, examine this short program.
#include <stdio.h>
main()
{
int *p, q;
q = 100; /* assign q 100 */
p = &q; /* assign p the address of q*/
printf(“%d”, *p);/* display q’s value using pointer*/
return 0;
}
First, the line int *p, q; defines two variables: p, which is declared as an integer pointer, and q,
which is an integer. Next, q is assigned the value 100.
In the next line, p is assigned the address of q. You can verbalize the & operator as “address of.“
Therefore, this line can be read as: assign p the address of q. Finally, the value is displayed
using the * operator applied to p. The * operator can be verbalized as “at address”.
Therefore the printf( ) statement can be read as “print the value at address q,” which is 100.
When a variable value is referenced through a pointer, the process is called indirection. It is
possible to use the * operator on the left side of an assignment statement in order to assign a
variable a new value using a pointer to it. For example, this program assigns a value q indirectly
using the pointer p.
#include <stdio.h>
main()
{
int *p, q;
p = &q; /* get q’s address */
*p = 199; /* assign q a value using a pointer */
printf(“q’s value is %d”, q);
return 0;
}
Note: The type of the variable and type of pointer must match.
Operations Performed Using Pointers
(a) Assignment
One can assign an address to a pointer by:
(i) Using an array name or
(ii) Using the address operator
From the previous example, p1 is assigned the address of the beginning of the array which is cell
234.
From the previous example, p1 = 100 which is the value stored in location 234.
From the previous example, p1 is stored in address 3606 whose value is 234.
Program that uses a for loop that counts from 0 to 9. It puts in the numbers using a pointer.
#include <stdio.h>
main()
{
int i,*p;
p = &i;
for ( i =0; i <10; i++)
printf (“ %d ”, *p);
return 0;
}
#include<stdio.h>
main()
{
int u1, u2;
int v = 3;
int *pv;
u1 = 2 * ( v + 5 );
pv = &v;
u2 = 2 * (*pv + 5 );
printf(“ \n u1 = %d u2 = %d ”, u1, u2);
return 0;
}
Output
u1 = 16, u2 = 16.
Explain why.
Note
• Never use a pointer of one type to point to an object of a different type.
For example:
int q;
float *fp;
fp = &q; /* pointer fp assigned the address of an integer */
fp = 100.23; / address used for assignment */
• Do not use a pointer before it has been assigned the address of a variable. May cause
program to crash.
For example:
main()
{
int *p;
*p =10; */Incorrect since p is not pointing to anything */
…
}
The above is meaningless and dangerous.