0% found this document useful (0 votes)
40 views25 pages

Chapter Two

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

Chapter Two

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter Two

Pointers in C+
+
What is Pointer?
• In C++, pointers are variables that store the
memory addresses of other variables.
• It is also known as locator or indicator that points
to an address of a value.
• Pointers are symbolic representations of
addresses.
• The address of the variable you’re working with is
assigned to the pointer variable that points to the
same data type (such as an int or string).
• Every variable we declare in our program has an
associated location in the memory, which we call
the memory address of the variable.
Cont.
• 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.
• Example:
Cont.
• Here, 0x at the beginning represents the address in the
hexadecimal form.
• Notice that the first address differs from the second by 4 bytes, and the
second address differs from the third by 4 bytes, and so forth if we had
many variables.
• The difference is because the size of an int is 4 bytes in a 64-bit system.
• Note: You may not get the same results when you run the program.
This is because the address depends on the environment in which the
program runs.
• When we explain the previous slide program:
o We created a pointer variable with the name ptr, that
points to a string variable, by using the asterisk sign *
(string* ptr).
 Note that the type of the pointer has to match the type of the
variable we are working with.
o We used the & operator to store the memory address of
the variable called stud, and assign it to the pointer.
o Now, ptr holds the value of stud’s memory address.
Questions
1. What does a pointer store in C++?
A. The memory address of another variable
B. The value of another variable
C. The data type of another variable
D. The size of a variable

2. Create a pointer variable with the name ptr, that


should point to a string variable named food:
string food=“pizza”;
3. What does the following code output?
A. Burger
B. &food
C. ptr
D. Error
Syntax of Pointer Declaration
• The pointer in C++ language can be declared
using ∗ (asterisk symbol).
• datatype *var_name;
• int *ptr; // ptr can point to an address which holds
int data.
• There are three ways to declare pointer variables,
but the first way is preferred.
• string* stud; // The most Preferred method
• string *stud;
• string * stud;
How to use a pointer?
• Define a pointer variable.
• Assigning the address of a variable to a pointer
using the unary operator (&) which returns the
address of that variable.
• Accessing the value stored in the address using
unary operator (*) which returns the value of the
variable located at the address specified by its
operand.
Example
• Pointer Program to swap 2 numbers without using
3rd variable.

• Write a C++ pointer program to swap two


numbers using 3rd variables?
Solution for the exercise
Get the Value from the Address Using Pointers
• As we saw in the previous example, we use *
operator to get the value of a pointed by a
pointer.
• 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.
• Note: In C++, point_var and *point_var are
completely different. We cannot do something like
*point_var = &var;.
• Here, point_var is a pointer that stores the
address of variable it points to while *point_var
returns the value stored at the address pointed
by point_var.
Changing Value Pointed by Pointers
• If point_var points to the address of var, we can
change the value of var by using *point_var.
• For example, in the below code, point_var and
&var have the same address; the value of var will
also be changed when *point_var is changed.
Exercise
1. What happens if you change the value of a variable
through a pointer?
A. The original variable's value also changes
B. Only the pointer value changes
C. An error occurs
D. Nothing changes
Pointer Arithmetic
• A limited set of arithmetic operations can be
performed on pointers which are: incremented ( +
+ ) decremented ( -- ) an integer may be added
to a pointer ( + or += ) and compare.
Pointer Increment/Decrement (++, --)
• Incrementing a pointer (ptr++) moves it to the
next memory location of the type it points to.
• Decrementing a pointer (ptr--) moves it to the
previous memory location.
• The size by which the pointer moves depends on
the data type it points to.
• For example, if a pointer ptr points to an integer (size =
4 bytes on most systems), ptr++ will increment the
address by 4 bytes.
Example
Cont.
• Adding or subtracting an integer to a pointer
adjusts the pointer to point to a different memory
location.
• Example:
How Pointer Comparison Works
• To compare pointers, you should compare their
memory addresses (the locations they point to)
rather than the values they point to.
• Pointer comparison checks the relative positions
of the memory addresses. For example:
C++ 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.
Cont.
• Here, ptr is a pointer variable while arr is an int
array. The code ptr = arr; stores the address of
the first element of the array in variable ptr.
• Notice that we have used arr instead of &arr[0].
This is because both are the same.
• The addresses for the rest of the array elements
are given by &arr[1], &arr[2], and &arr[3].
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,

• If we use the dereference operator before the ptr, we


need to remove ampersand operator from the array
also. For example:
*(ptr+1) is equivalent to arr[1];
Cont.
• Note: The address between ptr and ptr + 1
differs by 4 bytes. It is because ptr is a pointer to
an int data. And, the size of int is 4 bytes in a 64-
bit operating system.
• Similarly, if pointer ptr is pointing to char type
data, then the address between ptr and ptr + 1 is
1 byte. It is because the size of a character is 1
byte.
Example of pointer and arrays
Relationship between pointers and Arrays
• Arrays and pointers are two derived data types in
C++ that have a lot in common.
• In some cases, we can even use pointers in place
of arrays. But even though they are so closely
related, they are still different entities.
• An array is the collection of multiple items of the
same type stored in contiguous memory locations.
– While declaring Arrays, the size should be mentioned and
their indexing starts from 0 in C++.
• A pointer is the symbolic representation of
addresses.
– It stores the address of variables or memory location.
Pointers enable programmers to create and manipulate
dynamic data structures.
Cont.
Exercises
1. What will be the output of the following
program?
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 4, 5, 6, 7 };
int* p = (arr + 1);
cout << *arr + 10;
return 0;
}
Cont.
2. What will be the output of the following
program?
#include <iostream>
using namespace std;
int main()
{
int num[5];
int* p;
p = num;
*p = 10;
p++;
*p = 20;
p = &num[2];
*p = 30;
p = num + 3;
*p = 40;
p = num;
*(p + 4) = 50;
for (int i = 0; i < 5; i++)
cout << num[i] << ", ";
return 0;
}

You might also like