You are on page 1of 16

Introduction to C Programming

Pointers
Topic & Structure of Lesson

In this chapter you will learn about:


• Pointers
• Introduction to Pointers
• Declaring Pointer Variables
• Accessing Data through Pointers

CT018-3-1 Introduction To C Programming Pointer


Learning Outcomes

If you have mastered this topic, you should be able


to use the following terms correctly in your
assignments :
 Pointer
 Pointer variable
 Address

CT018-3-1 Introduction To C Programming Pointer


Pointers

Pointer are a fundamental part of C. If you


cannot use pointers properly then you have
basically lost all the power and flexibility that C
allows. The secret to C is in its use of pointers.

CT018-3-1 Introduction To C Programming Pointer


Pointers

• Pointers are one of the most powerful features of


the C programming language
• They are also one of the most difficult capabilities
to master
• Pointers enable programs to simulate call by
reference, and to create and manipulate dynamic
data structures

CT018-3-1 Introduction To C Programming Pointer


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

CT018-3-1 Introduction To C Programming Pointer


Introduction to Pointers

• Pointers are variables that contain memory


addresses as their values
• Normally, a variable contains a specific value, while
a pointer contains an address of a variable that
contains a specific value
• Which means a variable name directly references a
value and a pointer indirectly references a value

CT018-3-1 Introduction To C Programming Pointer


Declaring Variables

int counter=100;
counter variable name

value of counter
100

x790 address of counter

printf(“\n Value of counter %d”, counter); //100


printf(“\n Address of counter %p”, &counter); //x790

CT018-3-1 Introduction To C Programming Pointer


Declaring Pointer Variables

Pointers like any other variables need to be


declared before they can be used.

int *ptr;

Declares the variable ptr which is a pointer type


variable and which can point to an object of type
integer.

CT018-3-1 Introduction To C Programming Pointer


Declaring Pointer Variables

• The unary & gives the “address of a variable”.


• The indirection or dereference operator * gives
the “contents of an object pointed to by a
pointer”.

CT018-3-1 Introduction To C Programming Pointer


Declaring Pointer Variables

Memory allocation

int counter=10; counter

10

x560
int *ptr; ptr

ptr=&counter;

x800

ptr indirectly references counter


whose value is 10

CT018-3-1 Introduction To C Programming Pointer


Declaring Pointer Variables

• In the previous slide, counter is declared at


address x560 and is assigned a value 10.
• ptr a pointer variable, is declared at address
x800.
• ptr is made to point to the address (&) of
counter. Which means it now contains x560 as
its value.

CT018-3-1 Introduction To C Programming Pointer


Accessing Data Through Pointers

#include<stdio.h>
void main() {
int a, *aPtr;
a=100;
aPtr=&a;
printf(“\n The address of a is %p”, &a);
printf(“\n The value contained in aPtr is %p”, aPtr);
printf(“\n The value of a %d and de-referencing
aPtr %d returns the same value”, a,*aPtr);
}

CT018-3-1 Introduction To C Programming Pointer


Accessing Data Through Pointers

#include<stdio.h>
void main()
{
char var=‘X’; // var a character variable, is declared and initialized to ‘X’
char *charPtr; //charPtr a pointer to a character is declared
charPtr=&var; //charPtr now holds (points to) the address of var
*charPtr=‘Y’; //The value which charPtr points to, is now changed to ‘Y’.
//using * in this manner is called dereferencing a pointer
printf(“\n Value of var now is : %c”,var);
// displays new value ‘Y’
}

CT018-3-1 Introduction To C Programming Pointer


Summary

In this chapter you learnt about:


• Introduction to Pointers
• Declaring Pointer Variables
• Accessing Data through Pointers

CT018-3-1 Introduction To C Programming Pointer


Question and Answer Session

Q&A

CT018-3-1 Introduction To C Programming Pointer

You might also like