CSE 06131223 CSE 06131224
Structured Programming
Lecture 22
Pointers in C (3)
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
Parameter Passing to Functions
• The parameters passed to function are called actual parameters. The parameters
received by function are called formal parameters.
• There are two most popular ways to pass parameters.
• Pass by Value: In this parameter passing method, values of actual parameters are copied
to function’s formal parameters and the two types of parameters are stored in different
memory locations. So any changes made inside functions are not reflected in actual
parameters of caller.
• Pass by Reference Both actual and formal parameters refer to same locations, so any
changes made inside the function are actually reflected in actual parameters of caller.
Functions in C 3
Parameter Passing to Functions
• Pass by value:
• Parameters are always passed by value in C. For example. in the below code, value of x is not
modified using the function fun().
Functions in C 4
Pointers as Function Arguments
• Pass by reference:
• In C, we can use pointers to get the effect of pass-by reference. For example, consider the below
program. The function fun() expects a pointer ptr to an integer (or an address of an integer). It
modifies the value at the address ptr. The address operator & is used to get the address of a variable
of any data type.
Functions in C 5
Pointers as Function Arguments
• Hence, pointers as function arguments in programming languages like C and C++ allow us to pass
a memory address instead of a value. This can be beneficial for several reasons:
• Passing by reference: By passing a pointer to a function, we can modify the original variable's value within
that function. This is particularly useful when we need to alter the value of a variable inside a function and
have those changes reflected outside the function.
• Reduced memory overhead: When we pass large data structures to functions, passing pointers instead of
the entire data structure can reduce memory usage and improve performance. This is because we're
passing just a memory address instead of copying the entire data.
• Efficiency: Pointers allow us to directly manipulate memory, which can be more efficient than copying large
data structures.
• Dynamic memory allocation: Pointers are essential when dealing with dynamically allocated memory.
Functions can use pointers to allocate and deallocate memory dynamically, allowing for more flexible
memory management.
Functions in C 6
Pointers as Function Arguments
• Here's a simple example in C:
• In this example, swap() takes two
integer pointers as arguments. Inside
swap(), it dereferences these pointers to
access and modify the values they point
to, effectively swapping the values of x
and y.
Functions in C 7
Function Returning Pointers
• Returning pointers from functions in languages like C and C++ can be powerful as it
allows functions to allocate memory dynamically and return the address of that
memory. Here's why returning pointers can be useful:
• Dynamic Memory Allocation: Functions can allocate memory dynamically using functions like
malloc() or new (in C++), and return a pointer to the allocated memory. This allows for flexible
memory management, especially when the size of the data to be returned is not known at compile
time.
• Returning Multiple Values: Pointers enable functions to return multiple values by pointing to a
data structure (e.g., an array or a struct) that holds these values. This is especially useful when you
need to return more than one value from a function.
• Accessing Dynamically Allocated Data: Functions can return pointers to dynamically allocated data
structures (e.g., arrays, linked lists, trees), allowing the caller to access and manipulate this data.
Functions in C 8
Function Returning Pointers
• Dynamic Memory Allocation and
Accessing Dynamically Allocated Data
• Here's a simple example in C:
• In this example, the createArray() function
dynamically allocates memory for an array
of integers, initializes its elements, and
then returns a pointer to the first element
of the array. The main() function calls
createArray() to get a pointer to the
dynamically allocated array, prints its
elements, and then frees the allocated
memory using free().
Functions in C 9
Function Returning Pointers
• Functions that return multiple values.
• In C, functions inherently return only one value. However, we can simulate the
concept of returning multiple values by using pointers.
• Using Pointers: Pass pointers to variables to the function, which will modify the
values stored at those memory locations.
Functions in C 10
Function Returning Pointers
• Functions that return multiple
values:
• Using Pointers: Pass pointers to
variables to the function
Functions in C 11
Pointers and Structures
• Using structures and pointers together in C is common and powerful. Pointers allow us to
dynamically allocate memory for structures, access and modify structure members efficiently,
and pass structures to functions by reference.
• The usage of structures and pointers together:
struct Student {
char name[50];
int age;
float gpa;
} *std;
• Accessing structure members:
std->name
std->age
std->gpa
Functions in C 12
Pointers and Structures
• Example of demonstrating how to use
structures and pointers together in C:
Structures and Unions in C 13
?
Pointers in C
THE END
14