Pointer in C++
Pointer in C++
C++
NAME: MUHAMMAD ALI
ROLL NO : 2149
SUBJECT: PROGRAMMING
FUNDAMENTALS
SUBMITED TO: SIR MUNIR-UL-
DIN
AGENDA
POINTER DEFINITION
POINTER ARTHIMETIC
CONSTANT POINTER
POINTER AND ARRAYS
Pointer in C++ 3
INTRODUCTION TO POINTER
A pointer is a variable that stores the memory address of an object. Pointers are used
extensively in both C and C++ for three main purposes: to allocate new objects on the
heap, to pass functions to other functions. to iterate over elements in arrays or other
data structures
eXPLAINATION
Every variable is allocated a section of memory large enough to hold a value of the variable’s data
type. On a PC, for instance, it’s common for one byte to be allocated for chars, two bytes for shorts,
four bytes for ints, longs, and floats, and eight bytes for doubles. Each byte of memory has a unique
address. A variable’s address is the address of the first byte allocated to that variable. Suppose the
following variables are defined in a program:
char letter;
short number;
float amount;
POINTER IN C++ 4
The definition of a pointer variable looks pretty much like any other definition. Here is an example: int
*ptr;
The asterisk in front of the variable name indicates that ptr is a pointer variable. The int data type
indicates that ptr can be used to hold the address of an integer variable. The definition statement
above would read “ ptr is a pointer to an int.”
PROGRAMM
// This program stores the address of a variable in a pointer.
2 #include<iostream>
3 using namespace std;
4
5 int main()
6{
7 int x = 25; // int variable
8 int *ptr = nullptr; // Pointer variable, can point to an int
9
Presentation title 5
OUTPUT
Program Output The value in x is 25
The address of x is 0x7e00
EXPLANATION
Presentation title 6
You learned in that an array name, without brackets and a subscript, actually represents
the starting address of the array. This means that an array name is really a pointer.
PROGRAMM
// This program shows an array name being dereferenced with the *
2 // operator.
3 #include<iostream>
4 using namespace std;
5
6 int main()
7{
8 short numbers[] = {10, 20, 30, 40, 50};
9
10 cout << "The first element of the array is ";
11 cout << *numbers << endl;
12 return 0;
13 }
Presentation title 7
OUTPUT
THE FIRST ELEMENT OF ARRAY IS 10
Because numbers works like a pointer to the starting address of the array, the first element is
retrieved when numbers is dereferenced. So how could the entire contents of an array be retrieved
using the indirection operator? Remember, array elements are stored together in memory, as
illustrated . It makes sense that if numbers is the address of numbers[0], values could be added to
numbers to get the addresses of the other elements in the array. It’s important to know, however, that
pointers do not work like regular variables when used in mathematical statements. In C++, when you
add a value to a pointer, you are actually adding that value times
PROGRAMM
Presentation title 8
OUTPUT
Enter 5 numbers:
5 10 15 20 25 [Enter]
Here are the numbers you entered: 5 10 15
20 25
Presentation title 10
OUTPUT
Here are the values in the coins array:
0.05 0.1 0.25 0.5 1
POINTER ARTHIMETIC
Some mathematical operations may be performed on pointers.
Presentation title 12
EXPLAINATION
The contents of pointer variables may be changed with mathematical statements that perform
addition or subtraction. This is demonstrated in Program 9-9 . The first loop increments the
pointer variable, stepping it through each element of the array. The second loop decrements the
pointer, stepping it through the array backward.
PROGRAMM
1 // This program uses a pointer to display the contents of an array.
2 #include<iostream>
3 using namespace std;
4
5 int main() 6
{
7 const int SIZE = 8;
Presentation title 13
11
12 // Make numPtr point to the set array.
13 numPtr = set;
14
15 // Use the pointer to display the array contents.
16 cout << "The numbers in set are:\n";
17 for (count = 0; count < SIZE; count++)
18 {
19 cout << *numPtr << " ";
20 numPtr++;
21 }
22
23 // Display the array contents in reverse order.
24 cout << "\nThe numbers in set backward are:\n";
25 for (count = 0; count < SIZE; count++)
26 {
27 numPtr−−;
28 cout << *numPtr << " ";
29 }
30 return 0;
Presentation title 14
OUTPUT
INITIALLIZING POINTER
Pointers may be initialized with the address of an existing object.
EXPLAINATION
Pointers may be defined in the same statement as other variables of the same type. The
following statement defines an integer variable, myValue,
and then defines a pointer, pint, which is initialized with the address of myValue :
int myValue, *pint = &myValue;
And the following statement defines an array, readings, and a pointer,
marker, which is initialized with the address of the first element in the array:
double readings[50],
*marker = readings;
Of course,
a pointer can only be initialized with the address of an object that has already been defined.
The following is illegal because pint is being initialized with the address of an object that
does not exist yet:
int *pint = &myValue; // Illegal! int myValue;
Presentation title 17
PROGRAMM
OUTPUT
The numbers in set are: 5 10 15 20 25 30 35
40 The numbers in set backward are: 40 35
30 25 20 15 10 5
THANK YOU
MUHAMMAD ALI