0% found this document useful (0 votes)
10 views

Module 1 - Chapter 10 Pointers

The document discusses pointers in C programming. Pointers are variables that store memory addresses. The text defines pointer constants and variables, explains how to declare and initialize pointers, and how to access variables through pointers using the indirection operator.

Uploaded by

Aditya Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 1 - Chapter 10 Pointers

The document discusses pointers in C programming. Pointers are variables that store memory addresses. The text defines pointer constants and variables, explains how to declare and initialize pointers, and how to access variables through pointers using the indirection operator.

Uploaded by

Aditya Agarwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Pointers

Data types
1) Fundamental or Primitive
2) Derived
3) User defined

Pointers: Pointers are the 3rd of the derived types.

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

2) Direct Approach: Pointers


- Uses data addresses directly with ease and
flexibility of symbolic names
Pointers
• A pointer is a constant or a variable that contains an
address that can be used to access data.

• A pointer is a derived data type : a data type built


from one of the standard types

• Its value is any of the addresses available in the


computer for storing and accessing data

• Pointers are built on the basic concept of pointer


constants.
Pointer Constants
• First, compare character constants and pointer
constants
• Character constant – we can have character
constants from a universe of all characters
• For most computers, it is known as American
Standard Code for Information Interchange
(ASCII)
• A character constant can become a value and
can be stored in a variable Ex: char aChar = ‘G’;
Pointer constants
Character constants & variables char aChar = ‘G’;

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.

• How to save this address?

• We have already done it by scanf with address


operator (&) which extracts address for a variable.
• Address expression: An expression type in the unary
expression category, consists of an ampersand (&)
and a variable name.
Pointer values (Fig 10.4 contd)
12
Ex:- Write a program to print addresses as pointers

prog1.c

Pointer values (Fig 10.4 – Print Character Addresses)


To print addresses as pointers ➔ conversion code is %p
13
Integer Constants and variables
• In most computers, integers occupy either 2 or 4
bytes (B).
• Assume we are working on a system with 4-byte
integers. This means that each integer occupies 4
memory locations
• Which one is then used to find the address of the
variable?
• The address of a variable is the address of the first byte
occupied by that variable
• For characters, there is only one byte, so its location is
the address. For integers, the address is the first byte of
the 4.
Pointer values (fig 10.5 Integer constants & variables)
.
.
.

234560
234561
234562
234563

234564
234565
234566
234567
.
.
.

15
Pointer variables
Pointer variables

• We have seen pointer constants and pointer values

• We can also have a pointer variable

• To store the address of a variable into another


variable
Pointer variables

• Distinguish between a pointer variable and its value

• There is an integer variable whose name and location


are constant, the value may change as the program
executes

• There is also a pointer which has a name and a


location, both of which are constants
Pointer variables (fig 10.6)

19
Pointer variables (fig 10.7 Multiple pointers to a variable)

20
Pointer variables

• What if we have a pointer variable, but do not want


it to point anywhere?

• What is its value then?

• C provides a special null pointer constant NULL


defined in standard input-output <stdio.h> library.
Accessing variables through
pointers
The indirection operator *

• We have a variable and a pointer to the variable


• How can we use the pointer?
• C has indirection operator *
• When we dereference a pointer, we are using its
value to reference (address) another variable
• The indirection operator is a unary operator whose
operand must be a pointer value
• To access the variable a, through the pointer p, we
write *p
The indirection operator *
• Assume that the variable x is pointed to by two
pointers p and q

• So x, *p, *q – allow the variable to inspect when


used in RHS of the assignment operator

• When used in LHS, they change the value of x


The indirection operator
• Add 1 to variable a using pointers and other means
• Any one of these statement will do it , assuming that
the pointer p is properly initialized as p = &a;
a++; a= a+1; *p = *p + 1; (*p)++;
• (*p)++ need parenthesis because ++ has more
priority than *
• The parenthesis force dereference to occur and then
addition
• Without parenthesis, we add to the pointer first,
which would change the address
Figure 10-8
Accessing variables through pointers
(fig 10.9 Address & Indirection Operators)

➢ The indirection address operators are the inverse


of each other and when combined in an
expression, such as *&x, they cancel each other.

27
Pointer declaration and
definition
Figure 10-10 Pointer Variable Declaration

• We use asterisk to declare a pointer variable


Pointer declaration & definition

Figure 10-11 shows declaration of different pointer


variables.

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.

• When asterisk is used for declaration, it is associated with


a type.
Example: int* pa; int* pb;

• When used for redirection, the asterisk is an operator


that redirects the operation from the pointer variable to
a data variable.
Example: sum = *pa + *pb;

34
Initialization of pointer variables
Initialization of pointer variables

• C language does not initialize variables

– when we start our program uninitialized variables have


garbage values.
Figure 10-12 Uninitialized variables and Pointers
Initialization of pointer variables

• C language does not initialize pointer variables


• When the program starts, uninitialized pointer
variables will have garbage address

• To solve the problem we need to assign a valid


memory address to the pointer
• Example:
int a;
int *p= &a; // p has valid address
*p = 90; // a is assigned 90
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;

printf(“%d %d %d\n”, a, b, c);


printf(“%d %d %d\n”, *p, *q, *r);
return 0;
} 42
Add two numbers using pointers (prog 10.3)
int main (void)
{
int a, b, r;
int *pa = &a;
int *pb = &b;
int *pr = &r;

printf(“Enter the first number : ”);


scanf(“%d”, pa);
printf(“Enter the second number : ”);
scanf(“%d”, pb);
*pr = *pa + *pb;
printf(“\nThe sum is : %d\n”, *pr);
return 0;
}

43
Figure 10-15 Demonstrate pointer flexibility

• We can use one pointer to print value of different variables.


Using one pointer for many variables (prog 10.4)
int main (void)
{
int a, b, c;
int *p;
Output:
printf(“Enter three numbers : ”);
scanf(“%d %d %d”, &a, &b, &c); Enter three numbers:
p = &a; 10 20 30
printf(“%d\n”, *p);
p = &b; 10
printf(“%d\n”, *p);
p = &c; 20
printf(“%d\n”, *p);
return 0; 30
}
45
Figure 10-16 One variable with many pointers

• We can use different pointers to print the value of the


same variable
Using a variable with many pointers (prog 10.5)
int main (void)
{
int a;
int *p = &a;
int *q = &a;
int *r = &a;

printf(“Enter a number : ”);


scanf(“%d”, &a);
printf(“%d\n”, *p);
printf(“%d\n”, *q); Output:
printf(“%d\n”, *r); Enter a number: 15
return 0; 15
}
15
15
47
Pointers and Functions
Pointers for Inter-function communication
• We study about how pointers can be used in functions.
• Exchange program without pointers

Passing addresses (fig 10.17) 49


Pointers for Inter-function communication
Passing addresses (fig 10.17 contd)

50
• Here we call the exchange function, passing it two
variables whose values are to be exchanged.

• When we use downward communication, the data are


exchanged in the called function, but nothing changes
in the calling program.

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

• It is important to understand that C still uses pass by value


– Now the value is the address of the variable we need to
change
– We are only simulating pass by reference
Pointers and Functions
➢ If we want a called function to have access to a
variable in the calling function send the address of
that variable to the called function and use the
indirection operator * to access it

➢ To send back more than one value from a function,


use pointers

➢ By passing the address of variables defined in calling


function, we can store data directly in the calling
function rather than using return
Pointers for Inter-
function communication
Exchange program with pointers

Passing addresses (fig 10.18 the correct way) 55


Functions Returning Pointers
• Functions returning a pointer to the calling
function

• Example: program to find the smaller of two


numbers by taking address of two numbers as
input and returning address of smaller number
as output
Functions returning pointers (fig 10.19)

58
Functions Returning Pointers – Points to note

• It is a serious error to return a pointer to a local


variable

• When we return a pointer, it must point to data in


the calling function or a higher level function

• It is an error to return a pointer to a local variable in


the called function because
– When the function terminates, its memory may
be used by other parts of the program
– Especially evident in large programs
Pointers to Pointers
Pointers to Pointers
• All pointers have been pointing directly to
data
• Advanced data structures require Pointers
that point to other pointers
Ex: we can have a pointer pointing to a
pointer to an integer
• This two-level indirection is shown next

Note: Although many levels of indirection can be used


but practically not more than two levels are needed
Figure 10-20 Pointers to pointers
Pointers to Pointers
• Each level of pointer indirection requires a separate
indirection operator when it is dereferenced
• Ex: To refer to a using the pointer p, we have to
dereference it once as *p

• To refer to a using the pointer q, we need to


dereference it twice to get to the integer a as there
are 2 levels of indirection (pointers) involved

• By first dereference, we reference p, which is a


pointer to an integer. q is a pointer to a pointer to an
integer **q
Pointers to Pointers
• Program:
• Write a program to print the value of a by 4 means:
– Directly using a
– Using pointer p
– Using pointer q
– Using pointer r

Figure 10-21 Using Pointers to Pointers


Pointers to Pointers (fig 10.6)

65
Pointers to Pointers (fig 10.6 contd)

66
End

You might also like