0% found this document useful (0 votes)
7 views15 pages

C++ Lecture Seven

This document is a lecture on pointers in C++, covering their definition, declaration, assignment, and arithmetic. It explains how pointers work, including the use of the address and dereferencing operators, and provides examples of pointer usage and functions that manipulate pointers. The lecture also touches on call by reference and call by pointer techniques in programming.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

C++ Lecture Seven

This document is a lecture on pointers in C++, covering their definition, declaration, assignment, and arithmetic. It explains how pointers work, including the use of the address and dereferencing operators, and provides examples of pointer usage and functions that manipulate pointers. The lecture also touches on call by reference and call by pointer techniques in programming.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

dfdfdfdfdfdfddfdfdf

Programming Fundamentals

Pointer

Dr. Ahmed Alnasheri 1/16/2021

Programming Fundamentals (Lecture Seven) 1


Pointers in C++

Topics to cover:

• Overview of Pointers
• Pointer Declaration
• Pointer Assignment
• Pointer Arithmetic

Programming Fundamentals (Lecture Seven) 2


Overview of Pointers
• A Pointer in C++ is variable whose value is a memory
address.
• With pointers many memory locations can be
referenced.
• Some data structures use pointers (e.g. linked list,
tree).
• The * and & operators
- & operator is the address operator
- * operator is the dereferencing operator. It is used in
pointers declaration

Programming Fundamentals (Lecture Seven) 3


Pointer Declaration
• Pointers are declared as follows:
<type> * variable_name ;
e.g.
int * xPtr; // xPtr is a pointer to data of type integer

char * cPtr; //cPtr is a pointer to data of type character

void * yPtr; // yPtr is a generic pointer,


// represents any type

Programming Fundamentals (Lecture Seven) 4


Pointer Assignment
• Assignment can be applied on pointers of the same type
• If not the same type, a cast operator must be used
• Exception: pointer to void does not need casting to convert a
pointer to void type
• void pointers cannot be dereferenced
• Example
int *xPtr, *yPtr; int x = 5;

xPtr = & x; // xPtr now points to address of x
yPtr = xPtr; // now yPtr and xPtr point to x

Programming Fundamentals (Lecture Seven) 5


Cont. Example

1. int *xPtr, *yPtr; int x = 5;

2. xPtr = & x;

3. yPtr = xPtr;

Programming Fundamentals (Lecture Seven) 6


Cont. Example
4. cout << *xPtr;
// this statement should print the value that is
pointed to by xPtr, which is 5.

5. cout<< xPtr;
// this statement should print the contents of xPtr
location, which is the address 1008.

- The same applies for yPtr, since it points to the


same location as xPtr.
Programming Fundamentals (Lecture Seven) 7
Pointer Arithmetic

• Increment / decrement pointers ( ++ or -- )


• Add / subtract an integer to/from a pointer
( + or += , - or -= )
• Pointers may be subtracted from each other
• Pointer arithmetic is meaningless unless
performed on an array
• This will be discussed in more details in Arrays
chapter.

Programming Fundamentals (Lecture Seven) 8


Examples on Pointers
//File: [Link]
//A program to test pointers and references
#include <iostream.h>
void main ( )
{ int intVar = 10;
int *intPtr; // intPtr is a pointer
intPtr = & intVar;
cout << "\nLocation of intVar: " << & intVar;
cout << "\nContents of intVar: " << intVar;
cout << "\nLocation of intPtr: " << & intPtr;
cout << "\nContents of intPtr: " << intPtr;
cout << "\nThe value that intPtr points to: " << * intPtr;
}

Programming Fundamentals (Lecture Seven) 9


Call by Reference
#include <iostream.h>
void Increment(int&);

void main() {
int A = 10;
Increment(A);
cout<<A<<endl;
}

void Increment(int &X) { ++X; }

Programming Fundamentals (Lecture Seven) 10


Call by Reference
//if you add extra cout statements to the program, the output will become:
#include <iostream.h>
void Increment(int&);
void main() {
int A = 10;
cout << “in main “<< &A << endl;
Increment(A);
cout<<A<<endl;
}
void Increment(int &X) { ++X;
cout << “in fun “<< X << endl;
cout << “in fun “<< &X << endl;
}

Programming Fundamentals (Lecture Seven) 11


Call by Pointer
#include <iostream.h>
void Increment(int*);

void main() {
int A = 10;
Increment(&A);
cout<<A<<endl;
}

void Increment(int *X) { ++*X; }

Programming Fundamentals (Lecture Seven) 12


Call by Pointer
//if you add extra cout statements to the program, the output will become:

#include <iostream.h>
void Increment(int*);
void main() {
int A = 10;
cout << "in main "<< &A<< endl;
Increment(&A);
cout<<A<<endl;
}
void Increment(int *X)
{ ++*X;
cout << "in fun X "<< X << endl;
cout << "in fun *X "<< *X << endl;
cout << "in fun &X"<< &X <<endl;
}

Programming Fundamentals (Lecture Seven) 13


Examples on Pointers (Cont.)
//File: [Link]
//A program to call a function to swap two numbers using reference parameters

#include <iostream.h>
void swap(int *, int *); // This is swap's prototype
void main()
{ int x = 5, y = 7;
swap(&x , &y); // calling swap with reference parameters
cout << "\n x is now "<< x << " and y is now " << y << '\n';
}
// swap function is defined here using dereferencing operator ‘*’
void swap(int *a, int *b)
{ int temp;
temp = *a; // here the values which a and b point to are swapped
*a = *b;
*b = temp;
}

Programming Fundamentals (Lecture Seven) 14


The End

Programming Fundamentals (Lecture Seven) 15

You might also like