0% found this document useful (0 votes)
14 views15 pages

Lecture 12 Pointers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

Lecture 12 Pointers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

KCA University

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

Introduction to Programming Lecture Note By Dr. L. A. Aloo


POINTERS

2
LECTURE 12
WHAT IS A POINTER?

•A pointer is a variable which contains the


address in memory of another variable.

•Definition: A pointer is a variable containing the


address of another variable.

•We can have a pointer to any variable type.

•The unary or monadic operator & gives the


``address of a variable''.
•The indirection or dereference operator *
gives the ``contents of an object pointed to by
3
a pointer''.
DECLARATION OF A POINTER
 The basic syntax to define a pointer is:
int *ptr;
This declares ptr as the identifier of an object
of the following type:
 pointer that points to an object of type int

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.

 p is assigned the address of q( & operator as


“address of” this line can be read as: assign
p the address of q).The value is displayed
using the *operator applied to p.
 printf( ) statement; “print the value at
address q” which is 100.when a variable
value is referenced through a pointer, the
process is called indirection. 11
EXAMPLE 2
#include<stdio.h>
int main ( )
{
int *p,q;
p=&q; //get q’s address
p=199; //assign q a value using a pointer
printf(“ q’s value is%d”,q);
return 0;
}

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

 Addition and subtraction of pointers

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

You might also like