KE06603 Engineering Programming: Lecture 14: Pointers
KE06603 Engineering Programming: Lecture 14: Pointers
Programming
Lecture 14: Pointers
Pointers
Pointers:
A pointer is a special type of variable for
storing the memory location or
address of another variable
pointers shouldnt be scary or confusing
provided they are used correctly
pointers can make your code more
compact and efficient
Memory Structure
In order to fully understand how
pointers are used to reference data
in memory, heres a few basics on
memory organization.
Memory
computer memory is a large array of consecutive
data cells or bytes
a char normally occupies one byte, a short 2 bytes,
an int or float 4 bytes, a double 8 bytes, etc.
when a variable is declared, the operating system
finds a place in memory to store the appropriate
number of bytes.
if we declare a variable called k, the place where k is
stored (also called the address of k) is denoted by
&k
it is convenient to print memory addresses in
Hexadecimal notation
Variables in Memory
int k;
int m;
printf( "address of k is %X\n", &k );
printf( "address of m is %X\n", &m );
address of k is BFFFFB80
address of m is BFFFFB84
This means that k occupies the four bytes
from BFFFFB80 to BFFFFB83,
and m occupies the four bytes from BFFFFB84
to BFFFFB87.
Arrays in Memory
When an array is declared, the elements of the array are
guaranteed to be stored in consecutive memory locations:
int array[5];
for( i=0; i < 5; i++ ) {
printf("address of array[%d] is %X\n", i, &array[i]);
}
address
address
address
address
address
of
of
of
of
of
array[0]
array[1]
array[2]
array[3]
array[4]
is
is
is
is
is
BFFFFB60
BFFFFB64
BFFFFB68
BFFFFB6C
BFFFFB70
Pointers
Suppose we have a pointer p that
points to a char variable c.
Assuming that the pointer p requires
2 bytes to store the address of c,
here is what the memory map might
look like:
The * Notation
Now that we have assigned to p the address
of variable c, we need to be able to
reference the data in that memory location.
operator * is used to access the object the
pointer points to. Hence to change the value
of c using the pointer p:
*p = T; // sets the value of c to T
Things to note:
All pointers are constrained to point to a
particular type of object.
// a potential pointer to any object of type char
char *s;
// a potential pointer to any object of type int
int *p;
Examples of Pointers
int *p; int *q; // this is how pointers are declared
int a[5];
int x = 10, y;
p = &x; // p now points to x
*p = 20; // whatever p points to is now equal to 20
y = *p; // y is now equal to whatever p points to
p = &a[2]; // p points to an element of array a[]
q = p; // q and p now point to the same thing
q++; // q now points to the next element of a[]
Example:
Pointers, address, indirection
name address
int a, b;
int *c, *d;
a = 5;
c = &a;
d = &b;
*d = 9;
print c, *c, &c
print a, b
memory
10
a 11
5?
b 12
9?
c 13
?
11
d 14
?
12
c=11
*c=5
a=5 b=9
&c=13
12
Exercise:
Trace the following code
name address
int x, y;
int *p1, *p2;
x = 3 + 4;
Y = x / 2 + 5;
p1 = &y;
p2 = &x;
*p1 = x + *p2;
*p2 = *p1 + y;
print p1, *p1, &p1
print x, &x, y, &y
memory
510
511 ?
512 ?
p 513 ?
1 514 ?
p2
13
Exercise
Show the memory snapshot after the
following operations
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
15
Call by Value
void swap(int a,int b)
{
int temp;
main()
{
int x = 2, y = 3;
temp = a;
a = b;
b = temp;
return;
}
printf("%d %d\n,x,y);
swap(x,y);
printf("%d %d\n,x,y);
}
Changes made in function swap are lost when the function execution is over
16
Call by reference
void swap2(int *aptr,
int *bptr)
{
int temp;
main()
{
int x = 2, y = 3;
printf("%d %d\n,x,y);
temp = *aptr;
*aptr = *bptr;
*bptr = temp;
return;
}
swap2(&x, &y);
printf("%d %d\n,x,y);
}
17
Trace a program
main()
{
int x0=5, x1=2, x2=3;
int *p1=&x1, *p2;
Name
Addr
x0
x1
x2
swap2(p1, p2);
p1
p2
Value
p2 = &x2;
swap2(&x0, &x1);
}
void swap2(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
return;
}
6
a
tmp
18
Exercise contd
main()
{
int a,b,c;
double root1,root2;
printf("Enter Coefficients:\n");
scanf("%d %d %d",&a,&b,&c);
computeroots(a,b,c,&root1,&root2);
printf("First Root = %lf\n",root1);
printf("Second Root = %lf\n",root2);
}
20