0% found this document useful (0 votes)
9 views29 pages

Week 13

The document provides an overview of pointers in C++, explaining their definition, declaration, and usage with examples. It covers key concepts such as dereferencing, pointer arithmetic, and the relationship between pointers and arrays. Additionally, it discusses common mistakes and introduces the concept of pointers to pointers for multiple levels of indirection.

Uploaded by

ahmad0708091
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)
9 views29 pages

Week 13

The document provides an overview of pointers in C++, explaining their definition, declaration, and usage with examples. It covers key concepts such as dereferencing, pointer arithmetic, and the relationship between pointers and arrays. Additionally, it discusses common mistakes and introduces the concept of pointers to pointers for multiple levels of indirection.

Uploaded by

ahmad0708091
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
You are on page 1/ 29

Basics of Pointer in C++

Lecture Link:
https://www.youtube.com/watch?v=eUw-
yAtp4BQ&list=PLVEVLI2v6thVDz7UxUPnURUKaqWFK7Z7v&index=35
Pointer

• A pointer however, is a variable that stores the


memory address as its value. A pointer variable
points to a data type (like int or string) of the
same type, and is created with the * operator.
The address of the variable you're working with
is assigned to the pointer.
Pointer Declaration
 we can declare pointers as
 int *pointVar;
 Here, we have declared a pointer pointVar of the int type.
We can also declare pointers in the following way as well.

 int* pointVar; // preferred syntax

 Note: The * operator is used after the data type to declare


pointers.
Example
 #include <iostream>
 #include <string>
 using namespace std;
 int main() {
 string food = "Pizza"; // A string variable
 string* ptr = &food; // A pointer variable that stores the address
of food
 // Output the value of food
 cout << food << "\n";
 // Output the memory address of food
 cout << &food << "\n";
 // Output the memory address of food with the pointer
 cout << ptr << "\n";
 return 0;
 }
Pointer as Dereference Operator
 A pointer references a location in memory,
and obtaining the value stored at that location
is known as dereferencing the pointer.
 you can also use the pointer to get the value of
the variable, by using the * operator (the
dereference operator). When * is used with
pointers, it's called the dereference operator. It
operates on a pointer and gives the value
pointed by the address stored in the pointer.
We discuss an example in this regard.
Example
 #include <iostream>
 using namespace std;
 int main() {
 int var = 5;
 // declare pointer variable
 int* pointVar;
 // store address of var
 pointVar = &var;
 // print value of var
 cout << "var = " << var << endl;
 // print address of var
 cout << "Address of var (&var) = " << &var << endl
 << endl;
 // print pointer pointVar
 cout << "pointVar = " << pointVar << endl;
 // print the content of the address pointVar points to
 cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar
<< endl;
 return 0;
 }
Pointer Working
Changing Value Pointed by Pointers

 If pointVar points to the address of var,


we can change the value of var by using
*pointVar.
 Here, pointVar and &var have the same
address, the value of var will also be
changed when *pointVar is changed.
Example
 #include <iostream>
 using namespace std;
 int main() {
 int var = 5;
 int* pointVar;
 // assign address of var
 pointVar = &var;
 // change value at address pointVar
 *pointVar = 1;
 cout << var << endl; // Output: 1
 }
Common mistakes when working
with pointers
 Suppose, we want a pointer varPoint to point to the address of var. Then,
 int var, *varPoint;
 // Wrong!
 // varPoint is an address but var is not
 varPoint = var;

 // Wrong!
 // &var is an address
 // *varPoint is the value stored in &var
 *varPoint = &var;

 // Correct!
 // varPoint is an address and so is &var
 varPoint = &var;

 // Correct!
 // both *varPoint and var are values
 *varPoint = var;
Pointer Arithmetic
int x =10 ;
int *yptr ;
yptr = &x ;
*yptr += 3 ;
yptr += 3 ;
Decrementing

*yptr --
Pointer Arithmetic
int *p1 ,*p2;
…..
p1 + p2 ;
Error
Pointers and Arrays

 Lecture Link:
 https://www.youtube.com/watch?v=VhryGzEe7sE&list=PLVEVLI2v6thVDz7UxUPnURUK
aqWFK7Z7v&index=36
Pointers and Arrays
 In C++, Pointers are variables that hold addresses of
other variables. Not only can a pointer store the address
of a single variable, it can also store the address of cells
of an array.

 Consider this example:


 int *ptr;
 int arr[5];
 ptr = arr; // store address of first element of arr in ptr
Cont..
 Notice that we have used arr instead of &arr[0].
This is because both are the same. So, the code
below is the same as the code previous.
 int *ptr;
 int arr[5];
 ptr = &arr[0];
 The addresses for the rest of the array elements
are given by &arr[1], &arr[2], &arr[3], and
&arr[4].
Point to Every Array Elements
 Suppose we need to point to the fourth element of the
array using the same pointer ptr.
 Here, if ptr points to the first element in the above
example then ptr + 3 will point to the fourth element. For
example,
 int *ptr;
 int arr[5];
 ptr = arr;
 ptr + 1 is equivalent to &arr[1];
 ptr + 2 is equivalent to &arr[2];
 ptr + 3 is equivalent to &arr[3];
 ptr + 4 is equivalent to &arr[4];
Working of C++ Pointers
with Arrays
Example
 // C++ Program to display address of each element of an array
 #include <iostream>
 using namespace std;
 int main()
 {
 float arr[3];
 // declare pointer variable
 float *ptr;
 cout << "Displaying address using arrays: " << endl;
 // use for loop to print addresses of all array elements
 for (int i = 0; i < 3; ++i)
 {
 cout << "&arr[" << i << "] = " << &arr[i] << endl;
 }
 // ptr = &arr[0]
 ptr = arr;
 cout<<"\nDisplaying address using pointers: "<< endl;
 // use for loop to print addresses of all array elements
 // using pointer notation
 for (int i = 0; i < 3; ++i)
 {
 cout << "ptr + " << i << " = "<< ptr + i << endl;
 }

 return 0;
 }
Pointers and Arrays
int y [ 10 ] ; Starting Address of Array y 0 [0]
1 [1]
2 [2]
3 [3]
4 [4]
5 [5]
6 [6]
7 [7]
8 [8]
9 [9]
The name of the array is like a
pointer which contain the
address of the first element.
location
3000 3004 3008 3012 3016

y[0] y[1] y[2] y[3] y[4]

pointer variable yPtr


In this case yptr is a pointer
to integer so now when we
increment yptr it points to
the next integer
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
int y [ 10 ] ;
int *yptr;
yptr = y ;
cout << yptr<<"\n" ;
yptr ++ ;
cout << yptr ;
}
yptr = y ;

is same as

yptr = &y [ 0 ] ;
……..
yptr = &y [ 2 ] ;
Example to differentiate yptr
and *yptr
#include <iostream>
#include <string>
using namespace std;
int main() {
int x = 10 ;
int *yptr ;
yptr = &x ;
cout << yptr<<"\n" ;
cout << *yptr ;

}
pointer to a pointer

 A pointer to a pointer is a form of


multiple indirection, or a chain of
pointers. Normally, a pointer contains
the address of a variable. When we
define a pointer to a pointer, the first
pointer contains the address of the
second pointer, which points to the
location that contains the actual value as
shown below.
Example
char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;
This, assuming the randomly chosen memory locations for
each variable of 7230, 8092, and 10502, could be
represented as:
Example Discussion
With the value of each variable represented inside its
corresponding cell, and their respective addresses in
memory represented by the value under them.

The new thing in this example is variable c, which is a


pointer to a pointer, and can be used in three different
levels of indirection, each one of them would correspond
to a different value:

c is of type char** and a value of 8092


*c is of type char* and a value of 7230
**c is of type char and a value of 'z'

You might also like