0% found this document useful (0 votes)
26 views14 pages

OOP Lecture 3

The document discusses pointers in C++ including using pointers to pass arguments by reference, access array elements, return multiple values from functions, perform dynamic memory allocation, and implement data structures. It provides examples of calling a function by reference using pointers, accessing single and multiple array elements with pointers, and pointer arithmetic.

Uploaded by

ayesha sabir
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)
26 views14 pages

OOP Lecture 3

The document discusses pointers in C++ including using pointers to pass arguments by reference, access array elements, return multiple values from functions, perform dynamic memory allocation, and implement data structures. It provides examples of calling a function by reference using pointers, accessing single and multiple array elements with pointers, and pointer arithmetic.

Uploaded by

ayesha sabir
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/ 14

OBJECT-ORIENTED Lecture 3

PARADIGM Instructor : Syeda Hira Fatima


POINTERS CONTD...
Use of POINTERS
•pass arguments by reference.(* dereferencing operator & reference operator)
•For accessing array elements.
•To return multiple values.
•Dynamic memory allocation.
•To implement data structures.
•To do system-level programming where memory addresses are useful.
Call by reference using pointers

# include<iostream> int main() {


# include<conio.h>
// initialize variables
using namespace std; int a = 1, b = 2;
//function definition
cout << "Before swapping" << endl;
to swap numbers
cout << "a = " << a << endl;
void swap(int* n1, int* cout << "b = " << b << endl;
n2) {
// call function by passing variable
int temp;
addresses
temp = *n1; swap(a, b);
*n1 = *n2;
cout << "\nAfter swapping" << endl;
*n2 = temp; cout << "a = " << a << endl;
cout << "b = " << b << endl;
} return 0;
Reference operator can be used while passing }
ACCESS ARRAY ELEMENTS USING POINTERS
A program that uses pointers to access a single element of an
array is given as follows

#include <iostream>
using namespace std;
int main()
{ int arr[5] = {5, 2, 9, 4, 1};
int *ptr = &arr[2];
cout<<"The value in the second index of the array is: "<< *ptr;
return 0; }
A single pointer is used to access all the elements of the array is given as
follows.

#include <iostream>
using namespace std;
int main()
{ int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
cout<<"The values in the array are: ";
for(int i = 0; i < 5; i++)
{ cout<< *ptr <<" ";
ptr++;
}
return 0;
}
SOME COMMANDS AND THEIR
MEANINGS
Int * p1 // Pointer declared
p1 =&firstvalue;// pointer points to first value
P1= var1// error as pointer can point to address or *p1 can access the value of the
variable
P1=& var 1// copies address of variable to the pointer.
*p1=10// stores the given value in pointer that is pointing to ?
Cout<< p1// Always displays address
Cout<<*p1 always displays value(pointed value or stored value)
*p1 = 10; // value pointed to by p1 = 10
p1 =&firstvalue;
Cout<<*p1 the value stored in the pointer is displayed.i.e.
POINTERS AND ARRAYS
The concept of arrays is related to that of pointers. In fact, arrays work very much like
pointers to their first elements, and, actually, an array can always be implicitly
converted to the pointer of the proper type. For example, consider these two
declarations:
int myarray [20];
int * mypointer;
The following assignment operation would be valid:
mypointer = myarray;
The main difference is
that mypointer can be assigned a
different address,
whereas myarray can never be
assigned anything, and will
always represent the same
block of 20 elements of
type int.
Therefore, the following assignment would not be valid:

myarray=mypointer;
a[5] = 0;
*(a+5) = 0;
POINTER ARITHMETICS
To conduct arithmetical operations on pointers is a little different than to conduct them on
regular integer types. To begin with, only addition and subtraction operations are allowed; the
others make no sense in the world of pointers. But both addition and subtraction have a slightly
different behavior with pointers, according to the size of the data type to which they point.
When fundamental data types were introduced, we saw that types have different sizes.
For example: char always has a size of 1 byte, short is generally larger than that,
and int and long are even larger; the exact size of these being dependent on the system. For
example, let's imagine that in a given system, char takes 1 byte, short takes 2 bytes,
and long takes 4.
EXAMPLE
Suppose now that we define three pointers in this compiler:
char *mychar; short *myshort; long *mylong;
and that we know that they point to the memory locations 1000, 2000, and 3000,
respectively.
++mychar; ++myshort; ++mylong;
mychar, as one would expect, would contain the value 1001. But not so
obviously, myshort would contain the value 2002, and mylong would
contain 3004, even though they have each been incremented
only once. The reason is that, when adding one to a pointer, the
pointer is made to point to the following element of the same
type, and, therefore, the size in bytes of the type it points to is
added to the pointer.
POINTERS AND CONSTANTS
Pointers can be used to access a variable by its address, and this access may include
modifying the value pointed. But it is also possible to declare pointers that can access
the pointed value to read it, but not to modify it. For this, it is enough with qualifying
the type pointed to by the pointer as const. For example:
int x;
int y = 10;
const int* p;// = &y;
p = &y;
x = *p; // ok:
reading p
cout << x;
//*p = x; //
error: modifying p, which
is const-qualified
return 0;

You might also like