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

BCSC 1102 Introduction to Programming Lecture 7

This document is a course outline for BCSC 1102: Intro to Programming, focusing on pointers in programming. It covers definitions, benefits, basic operations, types, scope, lifetime, and common pitfalls associated with pointers. The content is structured into sections with detailed explanations and examples for each topic related to pointers.

Uploaded by

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

BCSC 1102 Introduction to Programming Lecture 7

This document is a course outline for BCSC 1102: Intro to Programming, focusing on pointers in programming. It covers definitions, benefits, basic operations, types, scope, lifetime, and common pitfalls associated with pointers. The content is structured into sections with detailed explanations and examples for each topic related to pointers.

Uploaded by

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

BCSC 1102 : Intro To Programming Week 4

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

Contents
1 Introduction to Pointers 2
1.1 Definition of a Pointer . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Benefits of Using Pointers . . . . . . . . . . . . . . . . . . . . . . 2

2 Pointer Basics 2
2.1 Pointer Declaration and Initialization . . . . . . . . . . . . . . . 2
2.2 Pointer Dereferencing . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 NULL Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Pointer Arithmetic 3
3.1 Pointer Addition and Subtraction . . . . . . . . . . . . . . . . . . 3
3.2 Pointer Comparison . . . . . . . . . . . . . . . . . . . . . . . . . 3

4 Pointer Types and Casting 3


4.1 Void Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.2 Pointer to Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . 4

5 Scope and Lifetime of Pointers 4


5.1 Scope of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.2 Lifetime of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . 5

6 Common Pointer Pitfalls 5


6.1 Dangling Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
6.2 Wild Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6.3 Null Pointer Dereferencing . . . . . . . . . . . . . . . . . . . . . . 6

1
1 Introduction to Pointers
1.1 Definition of a Pointer
A pointer is a variable that stores the memory address of another variable.
Pointers are a powerful feature in C that allow for dynamic memory manage-
ment, efficient handling of variables, and direct memory manipulation.

1.2 Benefits of Using Pointers


• Direct Memory Access: Allows access to memory locations directly.
• Efficient Data Manipulation: Facilitates manipulation of large data
structures efficiently.
• Dynamic Data Structures: Essential for creating and manipulating
dynamic data structures like linked lists.

2 Pointer Basics
2.1 Pointer Declaration and Initialization
A pointer must be declared before it can be used to store the address of a
variable.
Syntax:
1 type * pointer_name ;

Example:
1 int * ptr ; // Declaration of an integer pointer
2 int x = 10;
3 ptr = & x ; // Initialization with the address of x

2.2 Pointer Dereferencing


Dereferencing a pointer means accessing the value stored at the memory address
the pointer holds.
Example:
1 int y = * ptr ; // y now holds the value of x (10)

2.3 NULL Pointer


A pointer that is not assigned any address is known as a NULL pointer. It is
good practice to initialize pointers to NULL to avoid accidental dereferencing.
Example:

2
1 int * ptr = NULL ;

3 Pointer Arithmetic
3.1 Pointer Addition and Subtraction
Pointers can be incremented or decremented to point to the next or previous
memory location of the type they point to.
Example:
1 int x = 10;
2 int * ptr = & x ;
3 printf (" Address of x : % p \ n " , ptr ) ;
4 ptr ++; // Points to the next memory location
5 printf (" New address : % p \ n " , ptr ) ;
6 ptr - -; // Points back to the original address of x
7 printf (" Address of x : % p \ n " , ptr ) ;

3.2 Pointer Comparison


Pointers can be compared using relational operators. This is useful for checking
relationships between memory addresses.
Example:
1 int x = 10 , y = 20;
2 int * ptr1 = & x ;
3 int * ptr2 = & y ;
4

5 if ( ptr1 < ptr2 ) {


6 printf (" ptr1 points to a lower memory address than
ptr2 \ n ") ;
7 } else {
8 printf (" ptr1 points to a higher or same memory
address as ptr2 \ n ") ;
9 }

4 Pointer Types and Casting


4.1 Void Pointers
A void pointer is a pointer that can hold the address of any data type. It is
useful for generic programming tasks.
Example:

3
1 void printIntValue ( void * ptr ) {
2 printf (" Integer value : % d \ n " , *( int *) ptr ) ;
3 }
4

5 int main () {
6 int x = 10;
7 void * ptr = & x ;
8 printIntValue ( ptr ) ; // Output : Integer value : 10
9 return 0;
10 }

4.2 Pointer to Pointer


A pointer to a pointer stores the address of another pointer. It is used for more
complex memory manipulations.
Example:
1 int x = 5;
2 int * ptr1 = & x ;
3 int ** ptr2 = & ptr1 ; // Pointer to pointer
4 printf (" Value of x : % d \ n " , ** ptr2 ) ; // Output : Value
of x : 5

5 Scope and Lifetime of Pointers


5.1 Scope of Pointers
The scope of a pointer defines the region of the program where the pointer is
accessible.

• Local Scope: Pointers declared within a function are local to that func-
tion and cannot be accessed outside it.

• Global Scope: Pointers declared outside all functions are global and can
be accessed by any function in the program.
• Block Scope: Pointers declared within a block (e.g., within {}) are local
to that block.

Example:
1 int globalVar ; // Global variable
2

3 void function () {
4 int localVar ; // Local variable

4
5 {
6 int blockVar ; // Block - scoped variable
7 }
8 }

5.2 Lifetime of Pointers


The lifetime of a pointer refers to the period during which the pointer exists in
memory and retains its value.
• Automatic (Local) Variables: These variables are created when the
function is called and destroyed when the function exits.
• Static Variables: These variables retain their value between function
calls. They are initialized only once and exist for the lifetime of the pro-
gram.
• Global Variables: These variables exist for the entire lifetime of the
program.
Example:
1 void function () {
2 int localVar = 0; // Automatic variable
3 static int staticVar = 0; // Static variable
4

5 localVar ++;
6 staticVar ++;
7

8 printf (" localVar : %d , staticVar : % d \ n " , localVar ,


staticVar ) ;
9 }
10

11 int main () {
12 function () ; // Output : localVar : 1 , staticVar : 1
13 function () ; // Output : localVar : 1 , staticVar : 2
14 return 0;
15 }

6 Common Pointer Pitfalls


6.1 Dangling Pointers
A dangling pointer arises when an object is deleted or deallocated, but the
pointer still points to the memory location of the deallocated memory. Accessing
a dangling pointer can lead to undefined behavior.
Example:

5
1 int * c r e a t e D a n g l i n g P o i n t e r () {
2 int x = 10;
3 return & x ;
4 }
5

6 int main () {
7 int * danglingPtr = c r e a t e D a n g l i n g P o i n t e r () ;
8 printf ("% d " , * danglingPtr ) ; // Undefined behavior
9 return 0;
10 }

6.2 Wild Pointers


Wild pointers are pointers that have not been initialized to a valid address.
They can cause a program to crash or behave unpredictably.
Example:
1 int main () {
2 int * wildPtr ; // Uninitialized pointer
3 * wildPtr = 10; // Undefined behavior
4 return 0;
5 }

6.3 Null Pointer Dereferencing


Dereferencing a NULL pointer results in a segmentation fault, causing the pro-
gram to crash.
Example:
1 int main () {
2 int * nullPtr = NULL ;
3 printf ("% d " , * nullPtr ) ; // Segmentation fault
4 return 0;
5 }

You might also like