You are on page 1of 15

CSE-125

Computer Programming &


Engineering Analysis

Lecture: 7
Pointers
Avishek Das

Department of CSE
CUET
POINTERS
Pointers
• A pointer is a derived data type in C.

• Pointers contain memory addresses as their values.

• Since these memory addresses are the locations in


the computer memory where program instructions
and data are stored, pointers can be used to access
and manipulate data stored in the memory.
ADVANTAGES
• Pointers provide alternative way to access variable/ memory block.
• Pointer increases the execution speed of the program.
• Pointers can be used with function to return more than one data
value.
• Pointers are used for DMA.
• Pointers save memory space.
UNDERSTANDNIG POINTERS
• The computer's memory is a sequential
collection of storage cells
• Each cell commonly known as a byte, has a
number called address associated with it
• The address are numbers consecutively,
starting from zero
• The last address depends on the
• memory size
• A computer system having 64K memory
will have its last address as 65,535
POINTER VARIABLE
• We may access the value 547 by using either the
name X or the address 4000.
• Since memory addresses are simply numbers, they
can be assigned to some variables, that can be stored
in memory, like any other variable.
• Such variables that hold memory addresses are called
pointer variables.
• A pointer variable is, nothing but a variable that
contains an address, which is a location of another
variable in memory.
ACCESSING THE ADDRESS OF A VARIABLE

• The actual location of a variable in the memory is system


dependent.
• We can determine the address of a variable with the help of
the operator & available in C.
• The operator & immediately preceding a variable returns the
address of the variable associated with it.
ptr = &x
• Would assign a address #### to the variable ptr
Example
Get the Value using Pointer
Get the Value using Pointer
Changing Value Pointed by Pointers
Chain of Pointers
Value Passing in Function

➢In pass by value, values of actual parameters are copied to the


variables in the parameter list of the called function. The called
function works on the copy and not on the original values of the
actual parameters. This ensures that the original data in the calling
function cannot be changed accidentally.

➢In pass by pointers, the memory addresses of the variables rather


than the copies of the values are sent to the called function. In this
case, the called function directly works on the data in the calling
function and the changed values are available in the calling function
for its use.

25/11/2022 Department of CSE, Chittagong University of Engineering & Technology 14


Pass by reference Using
Pointers

25/11/2022 Department of CSE, Chittagong University of Engineering & Technology 15

You might also like