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

Lecture 12 - Introduction to Pointers

Chapter 6 introduces pointers, explaining their differences from ordinary variables, how to declare and initialize them, and their usage in indirect referencing. It covers the importance of the indirection operator, valid and invalid operations with pointers, and the concept of NULL pointers for error handling. The chapter emphasizes the need for caution when dereferencing pointers and outlines the specific arithmetic operations that are permissible with pointer variables.

Uploaded by

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

Lecture 12 - Introduction to Pointers

Chapter 6 introduces pointers, explaining their differences from ordinary variables, how to declare and initialize them, and their usage in indirect referencing. It covers the importance of the indirection operator, valid and invalid operations with pointers, and the concept of NULL pointers for error handling. The chapter emphasizes the need for caution when dereferencing pointers and outlines the specific arithmetic operations that are permissible with pointer variables.

Uploaded by

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

Chapter 6

Introduction to
Pointers
1. Background and introduction to pointers
 Difference with ordinary variables

2. Declaring a pointer variable


 General syntax
 Example

3. Initializing a pointer variable


 The address of operator (&)
 Memory map of an initialized variable

4. Indirect reference through pointers


 The indirection operator (*)
 3 uses of * operator

5. Valid/Invalid Operations with pointers


 We know that functions, which can return a single
value back to its caller.
 Often, it is required to return multiple values from a
function, e.g.
 Find the 2 quadratic roots of an equation provided the
values of a, b and c.
 In this case, 2 values should be returned.

 A function can return multiple values by passing


output parameters (also called pass-by-reference)
 Output parameters are passed by Indirect addressing
using pointers.
 A pointer or pointer variable is a memory cell
that stores the address of a data item.
 Compare with ordinary scalar (non-pointer)
variables:
 Scalar variable stores the value of a data item.
 Pointer stores the address of a data item.

 The address of a data item is represented by ‘&’


operator.
 Like ordinary variables, a pointer variable should be
declared before using it in the program.
• General syntax of declaring a pointer:
• type *ptr_name;
• The operator * is pointer declaration operator.
• ptr_name is a pointer variable for storing address
of a variable with datatype=type.
• The value of the pointer variable ptr_name is a
memory address.

Examples
 int *ptr_int; // stores the address of an integer
variable.
 char *ptr_char; // stores the address of an char
variable.
 float* ptr_float; // stores the address of an float
variable.
 The address of a data item (variable) can be obtained
using the addressof operator ‘&’.
 Also called the ampersand operator.
 Simply place the addressof operator in front of the
data item’s name.
 Assuming intVar is an integer variable, recall the
usage of scanf statement:
 scanf(“%d”, &intVar);
 Reads an integer, and places on the memory address
represented by intVar.
 Also, try printing this value:
 printf(“%d”, &intVar);
 We can initialize a pointer by storing in it the address
of another variable.
 Example:

 Note that itemp can only store address of an integer


variable.
 The following statement is referred as: The pointer itemp
points to integer m.

 After this statement, the memory map can be visualized as


follows, where arrow points to the memory cell, whose
address is stored in itemp:

 Here, it is assumed that the address of m in memory is 1024.


 The above statement stores 1024 in itemp, i.e. itemp now
indirectly refers to the variable m.
 After initializing a pointer, we can indirectly access/change the
value at stored address, as shown in the following example.

 The * operator in this case is called the indirection operator or


dereference operator.
 This operation of using the * operator to access the contents of a
memory cell is call dereferring / indirecting a pointer variable.

 The Indirection operator is used to access the contents of a


memory cell through a pointer variable that stores its address.
 The above statement indirectly changes the contents of the memory
cell represented by m (i.e. 1024) using pointer itemp, which stores its
address.
 The effect of above statement can be visualized as follows:

 The indirection operator can be read as access the memory cell


by “following the pointer”.
 Once you follow the pointer, you reach the memory address of an
integer, therefore, the type of *itemp is integer.
 As the type of *itemp is integer, any operation valid with integers
can also be performed with *itemp.
 A couple of examples are given below.
 Note that we have seen 3 uses of the * operator:

Operator name Type Purpose


Multiplication operator Binary Multiply its operands

Pointer declaration Unary Declare a pointer variable; only


operator used once with the pointer while
declaring it.
Indirection / Unary Follow the pointer to access the
dereference operator pointed memory cell; always used
once the pointer has been
declared.

Note the difference clearly and never


confuse them together.
 Note that dereferring an uninitialized pointer results in
a runtime error. E.g.
 int *ptr; // an uninitialized pointer
 printf(“%d”, *ptr); // results in a runtime error.

 It is a good practice to set uninitialized pointer


variables to a special value, i.e. NULL, in order to
indicate that the pointer does NOT point to any
location in memory.
 int *ptr=NULL;
 Note NULL is case-sensitive.

 A pointer which stores NULL is called a NULL


pointer.
 We can check for a NULL pointer before accessing any
pointer variable.
 By doing so, we can perform error handling in pointer related
code e.g. dereference pointer variable only if it’s NOT NULL.
 Example:
int *pInt = NULL;
if(pInt != NULL) /*We could use if(pInt) as well*/
{ /*Some code*/}
else
{ /*Some code*/}
 Not all operations with ordinary scalar variables are
valid with pointer variables.
 The only valid operations with pointer variables are:
1. Adding/subtracting an integer to/from a pointer variable
(Increment/decrement is a special case of
addition/subtraction)
2. Subtracting 2 pointer variables of same type
3. Compound assignment operators with integers for
addition/subtraction (+=, -+)
4. Pointers comparison using relational operators (<, >, <=, >=,
==, !=)
 Valid arithmetic operations with pointers are NOT
performed the same way as ordinary scalar variables.
 The results of arithmetic operations depend on the type of
pointer. We will discuss it after we discuss array lecture.
1. All other operations such as the following are
INVALID with pointer variables.
1. Multiplication/division of a pointer with integers/other
pointers
2. Addition of 2 pointer variables
3. Compound assignment operators of types other than listed
previously (i.e. *=, /=, %=)
LIVE
LINK

You might also like