Module 1 - Chapter 10 Pointers
Module 1 - Chapter 10 Pointers
Data types
1) Fundamental or Primitive
2) Derived
3) User defined
2
Introduction
• Pointer constants
• Pointer variables
• Accessing variables through pointers
• Pointer declaration and definition
• Initialization of pointer variables
• Pointers and functions
• Pointers to pointers
3
Pointers - Concepts
• Every computer has addressable memory locations
Data Manipulation
1) Indirect Approach: Identifiers
- We use memory location addresses symbolically
– We assign identifiers to data and then manipulate
their contents through the identifiers
Here, aChar contains the value ‘G’ that was drawn from the
Universe of character constants. 7
Pointer constants (fig 10.3)
8
Character Constants and Pointer Constants
• Like character constants, pointer constants cannot be
changed
• The address for variable aChar is drawn from the set
of pointer constants for our computer
• Although addresses within a computer cannot change
(remains constant for the duration of the run)
• But the address of a variable will change for each run
of the program
• Thus it is necessary to refer to pointer variables
symbolically
Pointer values
• Pointer Constants are addresses in memory and are
drawn from the set of addresses for a computer.
• Exist by themselves. Cannot change them, only use
them.
prog1.c
234560
234561
234562
234563
234564
234565
234566
234567
.
.
.
15
Pointer variables
Pointer variables
19
Pointer variables (fig 10.7 Multiple pointers to a variable)
20
Pointer variables
27
Pointer declaration and
definition
Figure 10-10 Pointer Variable Declaration
30
Problem
• Write a program to define an integer variable a
• Define a pointer to integer and assign a’s address
• Print a and its address
• Print the pointer value containing the address of a
Pointer declaration & definition
(Prog 10.1 Demonstrate use of pointers)
#include<stdio.h> a 14 00135760
int main (void)
{
int a; p 00135760 00135890
int *p;
a = 14;
p = &a;
printf(“%d %p\n”, a, &a);
printf(“%p %d %d\n”, p, *p, a); Output …. ?
return 0;
}
32
Pointer declaration & definition
(Prog. 10.1 contd)
33
Declaration versus Redirection
• Asterisk (*) is used in these two contexts.
34
Initialization of pointer variables
Initialization of pointer variables
39
Initialization of pointer variables
• How to set pointer to null during definition or during
execution?
int *p = NULL;
• What happens when you dereference the pointer
when it is null (or null pointer) ?
– When we dereference a null pointer, we are using
address zero. A valid address in the computer
– Depending on OS, this can be the physical address
zero or can be the first address location in our
program area
– In some systems : A Runtime error, NULL is not a
valid address
Change Variables (prog 10.2)
int main (void)
{
int a, b, c;
int *p, *q, *r;
a = 6;
b = 2;
p = &b;
q = p;
r = &c;
41
Change Variables (prog 10.2)
p = &a;
*q = 8;
*r = *p;
*r = a + *q + *&c;
43
Figure 10-15 Demonstrate pointer flexibility
50
• Here we call the exchange function, passing it two
variables whose values are to be exchanged.
51
Pointers and Functions
• Most useful application of pointers is in functions
• How does C function operate?
– C uses pass by value concept
– This means that the only direct way to send something
back from a function is through return value
• How to simulate pass by address?
– We can simulate the pass by reference by passing an
address and using it to refer back to data in the calling
program
– When we pass by address, we are actually passing a
pointer to a variable
Pointers and Functions
• Pass pointers to the values
• Given a pointer to a variable anywhere in our memory
space
– whether it is local to a function or main() or a global
variable
– we can change the content of the variable
58
Functions Returning Pointers – Points to note
65
Pointers to Pointers (fig 10.6 contd)
66
End