C Programming
Introduction
week 11: Pointers
om
Lecturer : Cao Tuan Dung
.c
Dept of Software Engineering
Hanoi University of
ng
Technology
co
For HEDSPI Project
an
th
ng
Memory address
o
du
• Computer's memory is made up of
u
bytes. Each byte has a number, an
cu
address, associated with it.
• In the picture below, addresses 924
through 940 are shown.
1
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Memory address
The unary operator & gives the
address of a variable
#include <stdio.h>
int main(){
float fl=3.14;
om
printf("fl's address=%u\n", (unsigned int) &fl);
return 0;
}
.c
ng
co
an
th
ng
Exercise 12.1
o
du
• Write a C program to input three
u
integers. Set up a single pointer to
cu
point to each of these integers in
turn. Display the value dereferencing
the pointer.
2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 12.2
• Write a program that print out the
address (in hexadecimal format) of
first 5 elements of the array
predefined as below:
om
int a[7]= {13, -355, 235, 47, 67, 943, 1222} ;
.c
ng
co
an
th
ng
Declaring a pointer variable
o
du
type *variable_name;
u
cu
• A pointer is declared by adding a *
before the variable name.
• Pointer is a variable that contains an
address in memory.
• The address should be the address of
a variable or an array that we
defined.
3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Pointers
• Here ptr is said to point to the
address of variable c
C
… 7 3 4 …
om
172 173 174 175 176 177 178 179 180 181
.c
P
… 174 3 4 …
832 833 834 835 836 837 838 839 840 841
ng
co
an
th
ng
Referencing
o
du
• The unary operator & gives the
u
address of a variable
cu
• The statement: ptr = &c;
• assigns the address of c to the
pointer variable ptr, and now ptr
points to c
• To print a pointer, use %p format.
4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Referencing
int n;
int *iptr; /* Declare P as a pointer to int */
n = 7;
iptr = &n;
om
n
… 7 3 4 …
172 173 174 175 176 177 178 179 180 181
.c
iptr
ng
… 174 3 4 …
832 833 834
co 835 836 837 838 839 840 841
an
th
ng
Dereferencing
o
du
• The unary operator * is the
u
dereferencing operator
cu
• Applied on pointers
• Access the object the pointer points
to
• The statement: *iptr = 5;
puts in n (the variable pointed to by
iptr) the value 5
5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 12.3
• Write a program asking the value
from user for 3 float variable a, b, c.
Then add 100 to the content of them
by using just a pointer.
om
.c
ng
co
an
th
ng
Pass arguments by value
o
du
• The functions we saw until now
u
received their arguments “by value”
cu
• They could manipulate the passed
values
• They couldn’t change values in the
calling function
6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Wrong Swap
• A swap that gets integers as variables
does not change the value in the
original variables.
void swap(int x, int y)
{
om
int tmp = x;
x = y;
.c
y = tmp;
}
ng
co
an
th
ng
How can we fix it?
o
du
• We can define swap so it gets pointers
u
to integers instead of integers
cu
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
• We then call swap by swap(&x, &y);
• This is pass by reference
7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Caller Called
main swap
X X by value
Y Y
om
swap
.c
*X by reference
*Y
ng
co
an
th
ng
Exercise 12.4
o
du
• Write a function that takes three
u
variable (a, b, c) in as separate
cu
parameters and rotates the values
stored so that value a goes to be, b,
to c and c to a. Test this function in a
program
8
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 12.5
Introduce int variables x, y, z and int*
pointer variables p, q, r. Set x, y, z to
three distinct values. Set p, q, r to the
addresses of x, y, z respectively.
1) Print with labels the values of x, y, z, p, q, r,
om
*p, *q, *r.
2) Swapping values of x, y, z. Print with labels
.c
the values of x, y, z, p, q, r, *p, *q, *r.
3) Swapping values of p, q, r. Print with labels
the values of x, y, z, p, q, r, *p, *q, *r.
ng
co
an
th
ng
Exercises 12.6
o
du
• To increase salary for an employee,
u
write a function incomeplus that is
cu
based on the current salary and the
number of years passed from the
beginning years (must > 3) of
current salary.
• Test it in a program.
9
CuuDuongThanCong.com https://fb.com/tailieudientucntt