Professional Documents
Culture Documents
Lecture 02: Pointers: Engr. Muhammad Asad Lecturer (EE) Institute of Space Technology, Islamabad Muhammad - Asad@ist - Edu.pk
Lecture 02: Pointers: Engr. Muhammad Asad Lecturer (EE) Institute of Space Technology, Islamabad Muhammad - Asad@ist - Edu.pk
Lecturer (EE)
Institute of Space Technology, Islamabad
muhammad.asad@ist.edu.pk
What are Pointers?
• Variables that store addresses as their values.
• Variable name is direct reference to value.
• Pointer is indirect reference.
• Referencing via pointer is called indirection.
• Enables pass-by-reference.
• Used to create and manipulate dynamic data structures like linked
lists, queue etc.
Count
CountPtr Count
7
Declaration
• int *CountPtr;
• CountPtr is pointer to int
• Char *Ptr;
• Ptr is pointer to char.
Initialization
• Initialized to null
• int *CountPtr=NULL; or int *CountPtr=0;
• Known as null pointer
• Points to nothing
• 0 is the only integer that can be directly assigned to pointer
Pointer Operators
• Address operator (&)- obtains memory address of its operand (unary
operator).
• Indirection Operator (*)- represents a value to which the pointer
operand points(unary operator).
• int y=5;
• Int *yPtr=NULL; // declaring a null pointer
• *yPtr=9;
• cin>> *yPtr;
• Dereferencing a pointer means accessing the value stored in the location that
the pointer is pointing to.
Pass-by-Reference with Pointers
• Pass-by-Value: modify original values of the arguments in the caller
function.
• Pass- by-Reference: doesn’t actually pass anything. Pointer to
variable is passed by value and copied into function’s corresponding
parameter.
Passing Arrays to Functions
• Value of array’s name is also its address.
• Don’t need to use & operator
• Array name and size specified in function parameters.
• int sum(const int array[], int size)