Module 3
Module 3
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 1
• Pointers can be used to access and manipulate
the data stored in the memory.
• Pointers provide direct access to
memory.
• Pointers provide a way to return more
than one value to the functions.
• Reduces the storage space and
complexity of the program
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 2
• Pointers allows us to resize the dynamically allocated
memory block.
• Addresses of objects can be extracted using pointers
• Reduces the execution time of the program
• Provides an alternate way to access array elements
• Pointers can be used to pass information back and
forth between the calling function and called
function.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 3
• Pointers allows us to perform dynamic
memory allocation and deallocation.
• Pointers helps us to build complex data
structures like linked list, stack, queues,
trees, graphs etc.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 4
• Example:
• Int quantity=100;
• Now the variable stores the value
somewhere in the memory.
• Let us assume that 5000 is the address for
quantity.
• We can access 100 by either the name
quantity or by the address 5000.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 5
Since memory address are numbers, they can
be assigned to some variables, that can be
stored in memory, like any other variable.
Such variable that hold memory address are
called pointer variable.
•Pointer is nothing but variable that contains
an address, which is a location of another
variable in memory.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 6
• There are three underlying concepts
1. Pointer constant
2. Pointer value
3. Pointer variable
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 7
• Pointer constant
Memory address within a computer are referred to as pointer constant.
We can’t change them ;we can only use them to store data values.
We can’t save the value of memory address. we can get the value through the
variable stored there using the address operator(&).
• Pointer value
Pointer value may change from one run of the program to another.
Once we have a pointer value, it can be stored in another variable.
• Pointer variable
The variable that contains pointer value is called pointer varibles
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 8
Accessing the address of a variable
• The & operator can be used only with the simple variable or
an array element
• We can’t use like this:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 9
• If x is an array, then expressions such as
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 10
Example code
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 11
Declaring pointer variable
• Eg:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 12
• We can also declare
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 13
Initialization of a pointer
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 14
• We can also combine the initialization with the
declaration.
• Another style:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 15
• Pointers are flexible
• We can make the same pointer points to different data
variables in different statement
• We can also use different pointer points to same
variable.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 16
Accessing a variable through its pointer
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 17
POINTERS
So, the pointer variable declaration becomes something like
this,
int *pTonRate;
In other word, the pTonRate variable holds the memory
address of nRate and is therefore a pointer to nRate.
The asterisk (*) is used to show that is it the pointer
variable instead of normal variable.
Data_type * pointer_variable_name;
For example,
char* chName;
int * nTypeOfCar;
float *fValue;
int anotherVar;
int * pToInt;
pToInt = &nLocation;
nLocation = 100;
Then, statement,
anotherVar = *pToInt;
will place the actual current data value stored in variable nLocation into
variable anotherVar.
Which means anotherVar was assigned the actual current data value
stored at the memory address held by pToInt.
The * operator appears before a pointer variable in only two places:
Take note the difference between memory address and the actual data value,
which stored at the memory address.
Do not worry about the address, which are determined and provided by the
system.
From the previous example, we can:
1. Access the variable's content by using the variable name (iNum) and is
called direct access.
2. Access the variable's content by using a pointer to the variable
(*iPointerOne or *iPointerTwo) and is called indirect access or
indirection.
POINTERS
As a conclusion, if a pointer named pPointerVar of type int has
been initialized to point to a variable named iNumVar, the following
are true,
Hence, C reserved storage for the variable iAge and store the value
35 in it.
Let say, C has stored this value at the memory address 1000, then
we declare a pointer variable named pPointerToAge that point to
the iAge variable.
Then, after the following expressions have been executed,
int * pPointerToAge;
pPointerToAge = &iAge;
pPointerToAge++;
Operation Description
You can assign a value to a pointer. The value should be
1. Assignment (=) an address with the address-of-operator (&) or from a
pointer constant (array name)
The indirection operator (*) gives the value stored in the
2. Indirection (*)
pointed to location.
You can use the address-of operator to find the address of a
3. Address of (&)
pointer, so you can use pointers to pointers.
You can add an integer to a pointer to point to a different
4. Incrementing
memory location.
You can subtract an integer from a pointer to point to a
5. Differencing
different memory location.
6. Comparison Valid only with two pointers that point to the same array.
POINTERS
Uninitialized Pointers Issue
int *iPtrVar;
This statement declares a pointer to type int but not yet initialized, so it
doesn’t point to anything known value (address).
Then, consider the following pointer assignment statement,
For pointer that point to a string you may just point to an empty string for a
dummy such as,
www.tenouk.com, ©
Pointers to Pointers
POINTERS
Graphically, the construct of a pointer to pointer
can be illustrated as shown below.
iPointerOne is the first pointer, pointing to the
second pointer, iPointerTwo and finally
iPointerTwo is pointing to a normal variable
iNum that hold integer 100.
www.tenouk.com, ©
Advantages
Functions
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 39
• Elements of user defined functions we have
following steps
1.Definition of function.
2.Function call.
3.Declaring function.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 40
1.Function definition
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 41
• Having following elements.
1. Function name;
2. Function type;
3. List of parameters;
4. Local variable declaration;
5. Function statements;
6. A return statement;
• All these six elements are grouped into two
parts
• Function header(First three element)
• Function body(second three variable)
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 42
• A general format of a function definition to
implement these two parts:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 43
• The first line function_type
function_name(parameter list) is known as
the function header and the statements within
the opening and closing braces constitute the
function body.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 44
2. Function Call
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 45
• When compiler encounters function call, the
control is transferred to the function
definition.
• This function executed line by line and a
value is returned when a return statement is
encountered
• Syntax:
• Example: mul(x,y);
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 46
3. Declaring function
• Example
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 48
Call by value
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 49
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 50
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 51
Call by reference
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 52
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 53
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 54
Pointer as function argument
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 55
Recursion
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 57
To find factorial number
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 58
• How to calculate factorial number.
• n *fact(n-1)
• When we write an recursive functions, we
must have an if statement somewhere to force
the function to return without recursive call
being executed. otherwise function will never
return.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 59
Passing arrays to function
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 60
• After declaration we have to call the fuction
in main() program.In function call we need
not use any subscript as parameter. We are
directly pass the array variable name.
• Syntax: function_name(variable name1,….);
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 61
• After calling the function we have define the function to do
specific task. In the formal parameter we have to use array
name with subscripts and data type.
• Syntax:
• return_type function_name(datatype v_name[],datatype
v_name2….)
{
Code of block;
}
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 62
• Example:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 63
Two dimensional array
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 64
TWO DIMENSIONAL ARRAY
Syntax:
return_type function_name(data type [][], datatype,
datatype);
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 65
• Example:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 66
Passing string to function
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 67
Passing string to function
display(name);
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 68
Example:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 69
Functions returning pointer
• Function can return single value by its name or return multiple values
through pointer parameters.
• Since pointer is also a datatype in c, we can also force a function to
return a pointer to the calling function.
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 70
• Example:
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 71
Pointer to function
05/25/22 Mrs.Rakshithap_Assistant_Proffesor_Dept_MCA 72