Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Lecture 14-2

Advanced Programming
Shahanshah Abbas
shani@grel.ist.edu.pk

Advanced-Programming 1
Outline:
• Pointers
• Pointer with arrays
• Pointer withFunction
Pointers
• What are pointers:
• Computer memory consist of address
• For every Byte there is an address For This we Use Pointers
• Address like numbers 10,29,2299 etc.
• So when Code/ Program runs
• Occupies a specific range of the memory address
• Likewise every variable and function starts from a particular memory address
• Why we use pointers.
• Accessing Array elements
• Obtaining memory from system
• Passing arrays and strings to function
• Passing arguments to a function
• Producing data structures such a linked lists etc..
When Program Run
Pointers
• Address-of operator Address occupation in memory

• To find/check the address of a variable .

Address of operator

Addresses in hexadecimal
Pointers
But the int variable occupies 4 bytes in memory so what this address is showing

• Memory Address depend on


• Size of Operating system
• System on which the program is running
• Whether another program is running or not???
Pointer Variable
• Printing address value is not useful
• For conceptual and powerful programming, needs pointer variables
that hold memory address
• Pointer variable/ pointer
• Question: What is data type of pointer variable??
• Is the data type being same as that of the variable whose
address is being stored.????
• Like a pointer to Int variable is of type Int.
• Let's see a simple example of pointer variable
Pointer Variable Must have some value.
• The address like can be thought of as a pointer
constant
• Steps:
Pointer variables
are defined

Values are assigned


to pointer variables

Must be assigned a
variable address value
Values stored in PointerVariable
• Sometimes we need to access a variable value which is pointed by
pointer
• Let’s see.

Dereference operator or
indirection operator or
content of operator
Values stored in PointerVariable
• So far we have print the assigned variable addresses and values using
pointers.
• Pointers can also be used to perform any task just as we do on
variable directly like to assign a variable value

This * means declares


pointer variables

This * means values of


variables pointed to by
Something toremember.
• The address that you put in pointer should be of same type as pointer
variable.

What if I changed it to float from int


General Purpose pointers
The address that you put in pointer should
be of same type as pointer variable.
• Remember last slide issue.
• To address problem general purpose pointers can be used.
• Point to any datatype
• Called Pointer to void
• Used in specialized purposes like passing pointers to functions that
operate independently of any data type
• Let's see an example.
Pointers in Array’s
• Do you remember Arrays. 1D & 2D .
• Remember how the elements of array are accessed??

• Similarly Array Elements can also be using pointers!!!


Pointers withFunctions
• So far we have seen passing arguments to functions with values.
• There are others ways also
• Passing arguments by reference
• Passing arguments through pointers.
• Lets see it now.
• Can you pass 2D array to a function as reference arguments using
pointer variable.

You might also like