0% found this document useful (0 votes)
24 views18 pages

Lecture2.2 Basicsofc++programming

Pointers store the memory address of a variable rather than the variable's value. They allow access and modification of the variable being pointed to. Pointers are useful for passing arguments to functions by reference, accessing array elements, and dynamic memory allocation. Pointer arithmetic can be performed by adding or subtracting integers to a pointer to move it through memory addresses.

Uploaded by

paudelayush999
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
24 views18 pages

Lecture2.2 Basicsofc++programming

Pointers store the memory address of a variable rather than the variable's value. They allow access and modification of the variable being pointed to. Pointers are useful for passing arguments to functions by reference, accessing array elements, and dynamic memory allocation. Pointer arithmetic can be performed by adding or subtracting integers to a pointer to move it through memory addresses.

Uploaded by

paudelayush999
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 18

INTRODUCTION TO OOP

BASICS OF C++ PROGRAMMING


UNIT:2
2.15 Pointers:
Pointer variables declaration & initialization
Pointers are an important feature of C++ (and C), while many other languages, such as Visual Basic and Java,
python, etc. have no pointers at all (i.e. they work behind the scene, as a programmer you need not worry about
pointers).

You can do a lot of programming in C++ without using pointers, you will find them essential to obtaining the most
from the language.

What are pointers?


We know a variable is a container that stores certain values.
A pointer is a container but instead of storing a value, a pointer stores an address(i.e. the memory location).
Example:
Int main(){
int x=5;
cout<<x; o/p: 5
}
Q: x has its address inside the memory. i.e. its physical location. How can we get that location ??
A: cout<<&x; 0x…

Now,
Let‘s create a pointer that stores this address. SAME OR DIFFERENT ??
Int *ptr=&x;
cout<<ptr; o/p:0x….

Q: Can I access the value that is stored in the pointer variable??


A: Use dereference operator.
cout<<*ptr; o/p: 5
Q: I want to store the value in that variable. Is this possible??
A: Yes
*ptr=10;
cout<<*ptr; o/p: 10
cout<<x; o/p:??
Coding…
Note: Pointer has to be of same type to the variable it is pointing to.

Q:Why would I use a pointer just to assign the value to the variable??
A:The above example was just an example to demonstrate on pointers.
This is not the type of problems pointers were created to solve.
There are different problems pointer solve and are used for:
• Accessing array elements.
• Passing arguments to a function when the function needs to modify the original argument.
• Passing arrays and strings to functions.
• Obtaining memory from the system/Dynamic Memory Allocation.
• Creating data structures such as linked lists.
etc
2.16 Pointers and Arrays:
There is a close association between pointers and arrays.
Example:
#include <iostream>
using namespace std;
int main()
{
int intarray[5] = { 30, 40, 50, 60, 70 };
for(int j=0; j<5; j++) //for each element,
cout << intarray[j] << endl; //print value o/p:??
return 0;
}

Q:What will be the output of the following line of code??


cout<<intarray; ??
A: The name of our array is the address of the first element of that array.
Proof:
cout<<&intarray[0];
i.e. the name intarray behave as a pointer and the square bracket behaves as an operator for dereferencing.

So, cout<<intarray[2]; results 50

cout<<*(intarray+2);//adding two more addresses to the base. o/p??


Coding…

#include<iostream> cout<<"performing same action using


using namespace std; pointer"<<endl;
int main(){
int intarray[5];
cout<<"input values for an array"<<endl; for(int i=0;i<5;i++){
for(int i=0;i<5;i++){ //Input values for an array cout<<*(intarray+i)<<endl;
cin>>intarray[i]; }
}
cout<<"outputting values from an array"<<endl;
return 0;
for(int i=0;i<5;i++){
cout<<intarray[i]<<endl; }
}
2.17 Pointers and Functions:
This is similar to passing by reference, but the only difference is in this case, we can pass the address of a variable.
2.18 Dynamic Memory Allocation:
Need??
An array is a type of collection which stores element in continuos memeory, i.e. stores element one after another.

Ex: int main()


{
int myArray[5]; //Five containers of type int and these containers will be one after another in memory
}

Note: The size of an array has to be constant i.e. the size should be known at compile time.
i.e. int main()
{
int size;
cout<<‘‘Enter the size of an array‘‘;
cin>>size;
int myArray[size]; OUTPUT??
}
Solution:
DYNAMIC MEMORY ALLOCATION
2.19 Pointer Arithmetic:
As you understood pointer is an address which is a numeric value; therefore, you can perform arithmetic
operations on a pointer just as you can a numeric value.

ADDITION:
Ptr

myArray

0 1 2 3 4 5 6

Suppose we have a pointer ptr and we have an array myArray.


I want to point to the first element of an array. How??
Ptr=&myArray[0];
Q. What happens if I add some integers to the pointer, i.e. ptr=ptr+3 ??
A. We are moving pointers three position in forward direction.

i.e. Initially if Ptr points to myArray[0], then ptr=ptr+3 equals ptr=&myArray[0+3].


SUBTRACTION:
If ptr=&myArray[2], and ptr=ptr-2. Then ??
INCREMENT/DECREMENT:
Post Increment:
Pre Increment:
DECREMENT:

TRY IT YOURSELF

You might also like