Lecture 12 Pointers
Lecture 12 Pointers
Nairobi, Kenya
BSD 1207/BIT01203
INTRODUCTION TO PROGRAMMING
10/19/2024
Prepared By:
Dr. Linus Aloo, PhD
E-mail: [email protected]
Phone: 0754188380
Course Text
1. Programming in ANSI C" by E. Balagurusamy (8th Edition), McGraw Hill Education, 2019.
References
1. The C Programming Language" by Brian Kernighan and Dennis Ritchie (2nd Edition)
2. Fundamentals of programming languages by Dipali P. Baviskar, 2010.
3. Expert C Programming: Deep C Secrets" by Peter van der Linden.
4. C++: The Complete Reference" by Herbert Schildt (4th Edition)
1 5. https://www.tutorialspoint.com/cprogramming/index.htm
6. https://www.javatpoint.com/c-programming-language-tutorial
7. https://www.w3schools.com/c/index.php
2
LECTURE 12
WHAT IS A POINTER?
4
USES OF POINTERS
C uses pointers a lot. Why?:
It is the only way to express some
computations.
It produces compact and efficient code.
It provides a very powerful tool.
C uses pointers explicitly with:
Arrays,
Structures,
Functions.
5
EXPLANATION
•A variable is just a labelled place to store
some data.
•For example one can make variable, with
the
unimaginative name of ‘Variable1’, to store
an integer in C with the command
int Variable1;
•, store the number ‘96’ in it with
Variable1=96;
•and print it out with
printf("%d",Variable1); 6
CONT’ED
Instead of referring to this data store by name, one can
refer to it by its address in the computer memory.
This address can itself be put in another variable in
C, such a variable being called a ‘pointer’ because its
value points to where the value of the original
variable is rather than being such a value itself.
To do this one puts a ‘&’ infront of a variable to mean
“give me the address of ” and a ‘*’ infront of a
pointer to mean “give me whatever is that the
address ”.
So one could make point a pointer, unimaginatively
called ‘Pointer1’, to the above ‘Variable1’ by the
command;
7
Pointer1=&Variable1;
CONT’ED
which stores the address of ‘Variable1’ in the
pointer variable ‘Pointer1’. To copy the value
of ‘Variable1’ to another variable, ‘Variable2’,
one can use
Variable2=*Pointer1;
which would have the same effect as
Variable2=Variable1;
or print it out with
printf("%d",*Pointer1);
The syntax of creating a pointer variable in the
first place. For ‘Pointer1’ above, it would be 8
int *Pointer1;
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.
N/B: The * pointer operator has no relationship
to the multiplication operator,which uses the
same symbol.
9
EXAMPLE
#include<stdio.h>
int 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;
}
10
EXPLANATION
int *p,q; -defines two variables: p,which is
declared as an integer pointer, and q,which is
an integer.
q is assigned the value 100.
12
EXAMPLE 3
#include<stdio.h>
int main ( )
{
int x[3]={10,20,30};
int *p1,*p2;
p1=x; //assign address to a pointer
p2=& x[2]; //assign p2 to address of x[2]
printf(“ p1= %u,*p1=%d,&p1=%u \n”,p1,*p1,&p1);
return 0;
}
Output
P1=234,*p1=10,&p1=3606 13
OPERATIONS PERFORMED USING POINTERS
Assignment
One can assign an address to a pointer by: (i)using an
array name or (ii) using the address operator
Dereferencing(value finding)-the *operator gives the
value pointed to
Take a pointer address: the & operator tells us
where the pointer itself is stored
Incrementing or decrementing a pointer
14
EXAMPLE –COUNT NUMBERS USING A POINTER
#include<stdio.h>
int main ( )
{
int i,*p;
p=&i;
for(i=0;i<10;i++)
printf(“%d”, *p);
return 0;
}
15