Lecture SP20 Pointers in C 1
Lecture SP20 Pointers in C 1
Structured Programming
Lecture 20
Pointers in C (1)
Prepared by________________________________
Md. Mijanur Rahman, Prof. Dr.
Dept. of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University, Bangladesh
www.mijanrahman.com
Contents
Pointers in C
• What is Pointer in C?
• Memory Organization of Pointers
• Accessing the Address of a Variable
• Declaration of Pointer Variables
• Initialization of Pointer Variables
• Accessing a Variable Through Its Pointer
• Pointer Expressions
• Pointers and Arrays, and Array of Pointers
• Pointers and Strings
• Pointers and Functions
• Pointers and Structures
Pointers in C 2
Pointers in C
• A pointer is a derived data type in C. It is built from one of the fundamental data types available
in C. Pointers contain memory addresses as their values.
• In C, a pointer is a variable that stores the memory address of another variable. Instead
of storing the actual value of a variable, a pointer holds the location (address) in
memory where the variable is stored.
• This allows for dynamic memory allocation, efficient memory usage, and manipulation
of data indirectly.
Pointers in C 3
Pointers in C
• Pointers offer a number of benefits to the programmers. They include:
Pointers in C 4
Pointers in C
• Pointers are extensively used in C for various purposes, such as:
1. Dynamic Memory Allocation: Pointers are used to dynamically allocate memory during runtime using functions like malloc(), calloc(), and
realloc(). This enables programs to efficiently manage memory as needed, especially for data structures like arrays, linked lists, and trees.
2. Passing Parameters by Reference: Unlike passing parameters by value, which creates a copy of the data, passing parameters by reference using
pointers allows functions to modify the original data. This is particularly useful when dealing with large data structures or when multiple values
need to be returned from a function.
3. Manipulating Arrays: Pointers can be used to iterate through arrays efficiently. They can also be used to dynamically allocate multi-dimensional
arrays and to access elements in memory directly, bypassing array indexing.
4. String Manipulation: C-style strings are represented as arrays of characters terminated by a null character ('\0'). Pointers are commonly used to
manipulate strings efficiently, including tasks such as copying, concatenating, and comparing strings.
5. Implementing Data Structures: Pointers are essential for implementing various data structures such as linked lists, stacks, queues, trees, and
graphs. They allow for the creation of dynamic data structures that can grow or shrink as needed.
6. Function Pointers: Pointers can be used to store addresses of functions, allowing for the creation of callback mechanisms and implementing
advanced programming techniques like function pointers arrays, which are commonly used in event-driven programming.
7. Dynamic Data Structures: Pointers enable the creation of complex data structures with dynamic memory requirements, such as linked lists,
trees, and graphs. These data structures can adjust their size during runtime, leading to efficient memory usage and improved performance.
8. Pointer Arithmetic: Pointers can be manipulated using arithmetic operations like addition and subtraction, allowing for efficient traversal of data
structures and array manipulation.
Pointers in C 5
Pointers in C
• Here's a simple example to
illustrate pointers:
Pointers in C 6
Memory Organization
• The computer’s memory is a sequential collection of
storage cells as shown in the Figure. Each cell, commonly
known as a byte, has a number called address associated
with it.
• Typically, the addresses are numbered consecutively,
starting from zero. The last address depends on the
memory size.
• A computer system having 64KB memory will have its last
address as 65,535.
Pointers in C 8
Memory Organization
• Since a pointer is a variable, its value is also
stored in the memory in another location.
Pointers in C 9
Memory Organization
• Pointers are built on the three underlying concepts, such as:
• Pointer Constants
• Pointer Values
• Pointer Variables
• Memory addresses within a computer are referred to as pointer constants. We cannot change
them; we can only use them to store data values. They are like house numbers.
• We cannot save the value of a memory address directly. We can only obtain the value through
the variable stored there using the address operator (&). The value thus obtained is known as
pointer value; it may change from one run of the program to another.
• Once we have a pointer value, it can be stored into another variable. The variable that contains
a pointer value is called pointer variable.
Pointers in C 10
Accessing the Address of a Variable
• The actual location of a variable in the memory is system dependent, and, therefore, the
address of a variable is not known to us immediately.
• The address can be determined with the help of the operator & available in C. The
operator & immediately preceding a variable returns the address of the variable
associated with it. For example, the statement:
p = &quantity;
• Would assign the address 5000 (the location of quantity) to the variable p. The &
operator can be remembered as ‘address of’.
Pointers in C 11
Accessing the Address of a Variable
• Here's a simple C program that prints the
address of a variable along with its value:
Pointers in C 12
Declaring Pointer Variable
• The declaration of a pointer variable takes the following form:
data-type *ptr-name;
• This tells the compiler three things about the variable ptr-name.
1. The asterisk (*) tells that the variable ptr-name is a pointer variable.
2. ptr-name needs a memory location.
3. ptr-name points to a varable of type data-type.
• For example,
int *p;
float *x;
Pointers in C 13
Declaring Pointer Variable
• Pointer declaration style:
• The symbol * can appear anywhere between the type-name and the pointer variable-
name. We can use the following styles:
int* p;
int *p;
int * p;
Pointers in C 14
Initialization of Pointer Variables
• The process of assigning the address of a variable to a pointer variable is known as
initialization. Once a pointer variable has been declared, we can use the assignment
operator to initialize the variable. For example:
int number;
int *p;
p = &number;
• We can also combine the initialization with the declaration. That is,
int *p = &number;
is allowed.
Pointers in C 15
Initialization of Pointer Variables
• In C, pointer variables can be initialized in several ways:
• Initializing with Address of Another Variable: We can initialize a pointer variable with
the address of another variable using the address-of operator (&). For example:
int num = 10;
int *ptr = #
• Initializing with NULL: We can initialize a pointer variable to NULL if we don't want it to
point to any valid memory address initially. This is often done to indicate that the
pointer is not currently pointing to anything. For example:
int *ptr = NULL;
Pointers in C 16
Initialization of Pointer Variables
• Initializing during Declaration: Pointer variables can be initialized during declaration. For
example:
int num;
int *ptr = #
Pointers in C 17
Initialization of Pointer Variables
• String Literal Initialization: Pointers can be initialized with string literals, which are
arrays of characters. For example:
char *str = "Hello, C World!";
• Initializing with Another Pointer: Pointers can be initialized with the value of another
pointer of the same type. For example:
int *ptr1;
int *ptr2 = ptr1;
Pointers in C 18
• Here's a C program demonstrating various
ways to initialize pointer variables:
Pointers in C 19
?
Pointers in C
THE END
20