1 / 20 3 / 20
2 / 20 4 / 20
Address Operator Declaring Pointer Variables
The address of a variable can be referenced using the The C language allows us to store the address of a memory
address operator & location in a special type of variable called a pointer
This operator has always been used as part of a scanf variable (or simply pointer)
When a pointer is declared, the type of variable to which it
double x; will point must also be specified
declaration
scanf("%lf", &x);
When reading an input, scanf needs to know the location of type * varId ;
@200 ,
<double> x 1.23
C uses an asterisk in declarations to indicate that the
variable is a pointer variable
5 / 20 7 / 20
The memory representation for the declaration on the Consider the following statements:
previous slide is as follows: int count, *ptr;
@100 ptr = &count;
<int> count 1010 *ptr = 1020;
printf("Var at %d stores %d\n", ptr, *ptr);
@300
Unlike declarations, the asterisk used in statements is a
<int *> ptr ?
dereferencing or indirection operator
In the statement *ptr = 1020, the value 1020 is assigned
Since ptr is a variable, to the “variable pointed to by ptr”
The variable count now stores the value 1020
– it stores an unknown address value
In the printf statement, *ptr is the “value pointed to by
– it is itself located at memory address @300
ptr”, which is the same as the value of count
9 / 20 11 / 20
To specify that ptr should refer (or point) to the variable Now consider the following declarations:
count, we can assign the address of count (&count) to int count = 1020, *ptr1, *ptr2;
the variable ptr:
ptr = &count; @300
<int *> ptr1 ?
The memory representation is as follows:
@100 @300 @100
<int> count 1010 <int *> ptr @100 <int> count 1020
@400
<int *> ptr2 ?
To avoid using memory address values, a convenient way is
to use an arrow to point to the desired variable
10 / 20 12 / 20
More Pointer Assignments Pointer Assignment Errors
ptr1 = &count; The following are some common errors when working with
– Assign the address of variable count to the variable ptr1 pointers, suppose
int y, *ptr1, *ptr2;
@300
<int *> ptr1 @100
– Changing the address of an integer variable:
@100 &y = ptr1;
<int> count 1020
– Assigning pointer with a non-address value:
@400 ptr1 = y;
<int *> ptr2 ? ptr1 = *ptr2;
– Assigning an address to an integer variable:
y = ptr2;
*ptr1 = ptr2;
13 / 20 15 / 20
ptr2 = ptr1; To test whether two different pointers are pointing to the
same variable or not, we use relational operators == and !=
– Assign value of ptr1 to variable ptr2 to compare pointers
– A pointer can be assigned to another pointer of the
same type, i.e. several pointers can point to the same if (ptr1 == ptr2) {
printf("Both pointers point to the same variable\n");
location }
@300
Other relational operators <, >, <=, >= more relevant in the
<int *> ptr1 @100
context of arrays
@100 A pointer can be assigned to or compared with the constant
<int> count 1020 NULL (which is the integer zero) defined in <stdio.h>
@400 To zero a pointer, assign a NULL value to indicate that the
<int *> ptr2 @100 pointer has no address
ptr = NULL; // or ptr = 0;
14 / 20 16 / 20
Pointer Exercises More pointers... (non-examinable)
Give memory snapshots after each of these sets of Can we declare a pointer variable that points to another
statements is executed: pointer variable? Yes, we can!
declaration
(a)
int a = 1, b = 2, *ptr; type varId ;
(b)
,
int a = 1, b = 2, c = 5, *ptr;
ptr = &c; int count, *ptr1, **ptr2;
c = b;
a = *ptr; ptr1 = &count;
ptr2 = &ptr1;
**ptr2 = 1010;
17 / 20 19 / 20
Given the following program fragment Be able to declare pointers of the appropriate type
int i1, i2;
Be able to assign pointers with appropriate address values
int *p1, *p2; Be able to dereference a pointer to access the variable that it
i1 = 5; is pointing to
p1 = &i1; Pointer variables are no different from the typical primitive
i2 = (*p1)/2 + 10;
p2 = p1; variable; assignment and comparison works the same way
Always ensure type-consistency when assigning pointers
Trace the code and give memory snapshots of the following:
If you are new to pointers, devise and use memory addresses
– What is the value of i1 and i2? to assist in program traces first; then progress on to using
– What is the value of *p1 and *p2? arrows
– What is the value of &i1 and &i2?
– What is the value of p1 and p2?
18 / 20 20 / 20