0% found this document useful (0 votes)
75 views20 pages

KE06603 Engineering Programming: Lecture 14: Pointers

Pointers allow values in memory to be indirectly accessed by storing and referencing memory addresses. A pointer variable contains the address of another variable. The indirection operator * is used to dereference a pointer and access the value at the stored address. Functions can modify arguments passed by reference by passing a pointer to the argument rather than passing the argument's value directly. This allows changes to persist after the function call.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views20 pages

KE06603 Engineering Programming: Lecture 14: Pointers

Pointers allow values in memory to be indirectly accessed by storing and referencing memory addresses. A pointer variable contains the address of another variable. The indirection operator * is used to dereference a pointer and access the value at the stored address. Functions can modify arguments passed by reference by passing a pointer to the argument rather than passing the argument's value directly. This allows changes to persist after the function call.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

KE06603 Engineering

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

Size of Memory Address


Just like any other variable of a certain type, a variable
that is a pointer also occupies space in memory. The
number of memory cells needed depends on the
computers architecture. For example:
for an old computer, or a hand-held device with only 64KB of
addressable memory, a pointer only requires 2 memory cells
(i.e. 2 bytes) to hold any address from 0000 to FFFF16 =
6553510

for desktop machine with 4GB of addressable memory,


a pointer requires 4 memory cells (i.e. 4 bytes) to hold
any address from 00000000 to FFFFFFFF16 =
429496729510

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

The * operator (indirection operator)is


sometimes described as dereferencing the
pointer, to access the underlying variable.

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;

If pointer p is pointing to an integer


variable x, then *p can occur in any
context that x could.

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;

Pointers in Function References (!


IMPORTANT!)
In C, function references are call-by-value except
when an array name is used as an argument.
An array name is the address of the first element
Values in an array can be modified by statements within
a function

To modify a function argument, a pointer to the


argument must be passed
scanf(%f, &X); This statement specifies that the
value read is to be stored at the address of X

The actual parameter that corresponds to a


pointer argument must be an address or pointer.

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);
}

Changes made in function swap are done on original x and y and.


So they do not get lost when the function execution is over

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

printf(%d %d %d\n, x0, x1, x2);

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

Now we can get more than


one value from a function
Write a function to compute the roots of quadratic
equation ax^2+bx+c=0. How to return two roots?

void comproots(int a,int b,int c,


double *dptr1, double *dptr2)
{
*dptr1 = (-b - sqrt(b*b-4*a*c))/(2.0*a);
*dptr2 = (-b + sqrt(b*b-4*a*c))/(2.0*a);
return;
}
19

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

You might also like