You are on page 1of 14

Pointer

Micro-Project
On

‘Pointer in c++'
Tree Traversal
Submitted To

MSBTE
In Partial Fulfilment of Requirement of Diploma Of
Computer Engineering
Under I Scheme
Submitted By
Ms. DISHA DIPAK NAIK.
Ms. GAURI VISHNU MOCHEMADKAR.
Mr. SHUBHAM RAMDAS ADELKAR

Ms. SHIVALI SHASHIKANT DHUMAL.

Mr. ARYAN KALIDAS GAD.

Under The Guidance Of

Miss.Swapnali Teli
Prof. Mr.S.M.Mayekar

FOR ACADEMIC YEAR 2021-2022

YASHWANTRAO BHAONSALE POLYTECHNIC, SAWANTWADI.

1|Page
Pointer

MAHARASHTRA STATE BOARD OF TECHNICAL


EDUCATION

CERTIFICATE
This is to certify that,
Ms. DISHA DIPAK NAIK. Roll no. 26
Ms. GAURI VISHNU MOCHEMADKAR. Roll no. 30

Mr. SHUBHAM RAMDAS ADELKAR. Roll No. 35

Ms. SHIVALI SHASHIKANT . Roll no. 36


Mr. ARYAN KALIDAS GAD. Roll no. 37

Of THIRD semester of diploma in COMPUTER ENGINEERING of institute


Yashwantrao Bhonsale Polytechnic (1742) has completed the Micro Project
Data Structure
satisfactorily in subject Object Using`C' (22317)
Oriented Programming using C++ (22316) for
the academic year 2021 to 2022 as prescribed in the curriculum.

Subject Faculty HOD Principal

Seal of

Institution

2|Page
Pointer

INDEX

Sr. Contains Page


No. No.

1. Abstract 4

2. introduction 5

3. How to use pointer 6

4. Application of pointer 7

5. Advantages of pointer 8

6. Usage of pointer 9

7. Example of pointers 10-11

8. Summary 12

9. Conclusion 13

10. Reference 14

3|Page
Pointer

Abstract

In C++, a pointer refers to a variable that holds the address of another variable.
Like regular variables, pointers have a data type. For example, a pointer of type
integer can hold the address of a variable of type integer. A pointer of character
type can hold the address of a variable of character type.
You should see a pointer as a symbolic representation of a memory address. With
pointers, programs can simulate call-by-reference. They can also create and
manipulate dynamic data structures. In C++, a pointer variable refers to a variable
pointing to a specific address in a memory pointed by another variable.

4|Page
Pointer

Introduction

Pointers

Pointers are symbolic representation of addresses. They enable programs to


simulate call-by-reference as well as to create and manipulate dynamic data
structures. It’s general declaration in C/C++ has the format:

The pointer in C++ language can be declared using ∗ (asterisk symbol).

Syntax:

datatype *var_name;
int *ptr; //ptr can point to an address

Fig.1

5|Page
Pointer

How to use a pointer?

• Define a pointer variable


• Assigning the address of a variable to a pointer using 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.

The reason we associate data type to a pointer is that it knows how many bytes
the data is stored in. When we increment a pointer, we increase the pointer by
the size of data type to which it points.

Fig.2

6|Page
Pointer

Application of Pointers

Functions in C++ can return only one value. Further, all the variables declared in
a function are allocated on the function call stack. As soon as the function returns,
all the stack variables are destroyed.

Arguments to function are passed by value, and any modification done on the
variables doesn’t change the value of the actual variables that are passed.

Using pointers as function arguments helps to pass the variable’s actual address
in the function, and all the changes performed on the variable will be reflected in
the outer function.

References and Pointers

There are 3 ways to pass C++ arguments to a function:

• call-by-value

• call-by-reference with pointer argument


• call-by-reference with reference argument

7|Page
Pointer

Advantages of using Pointers


Here, are pros/benefits of using Pointers

• Pointers are variables which store the address of other variables in C++.
• More than one variable can be modified and returned by function using
pointers.
• Memory can be dynamically allocated and de-allocated using pointers.
• Pointers help in simplifying the complexity of the program.
• The execution speed of a program improves by using pointers.

Pointer Expressions and 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 += )
• an integer may be subtracted from a pointer ( – or -= )
• difference between two pointers (p1-p2)

8|Page
Pointer

Usage of pointer

• There are many usage of pointers in C++ language.


• 1) Dynamic memory allocation
• In c language, we can dynamically allocate memory using malloc() and
calloc() functions where pointer is used.
• 2) Arrays, Functions and Structures
• Pointers in c language are widely used in arrays, functions and structures.
It reduces the code and improves the performance.

9|Page
Pointer

Pointer Example
Let's see the simple example of using pointers printing the address and value.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int number=30;
6. int ∗ p;
7. p=&number;//stores the address of number variable
8. cout<<"Address of number variable is:"<<&number<<endl;
9. cout<<"Address of p variable is:"<<p<<endl;
10. cout<<"Value of p variable is:"<<*p<<endl;
11. return 0;
12. }

OUTPUT
Address of number variable is: 0x7ffccc8724c4
Address of p variable is: 0x7ffccc8724c4
Value of p variable is: 30

10 | P a g e
Pointer

Pointer Program to swap 2


numbers without using 3rd
variable

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=20,b=10,∗p1=&a,∗p2=&b;
6. cout<<"Before swap: ∗p1="<<∗p1<<" ∗p2="<<∗p2<<endl;
7. ∗p1=∗p1+∗p2;
8. ∗p2=∗p1-∗p2;
9. ∗p1=∗p1-∗p2;
10. cout<<"After swap: ∗p1="<<∗p1<<" ∗p2="<<∗p2<<endl;
11. return 0;
12. }

OUTPUT
Before swap: ∗p1=20 ∗p2=10
After swap: ∗p1=10 ∗p2=20

11 | P a g e
Pointer

Summary
• A pointer refers to a variable holding address of another variable.
• Each pointer has a valid data type.
• A pointer is a symbolic representation of a memory address.
• Pointers allow programs to simulate call-by-reference and create and
manipulate dynamic data structures.
• Arrays and pointers use a related concept.
• The array name denotes the array’s base.
• If you want to assign the address of an array to a pointer, don’t use an
ampersand (&).
• If there is no specific address to assign a pointer variable, assign it a
NULL.

12 | P a g e
Pointer

Conclusion

To summarize what was previously discussed, Java is an object-oriented


programming (OOP) language. Object orientation helps a developer to achieve a
modular, extensible, maintainable, and reusable system. To write good-quality
programs, a programmer must have a firm command of OOP concepts. Object-
oriented programming revolves around the concept of an object, which
encapsulates data and behavior acting on the data together.

13 | P a g e
Pointer

Reference

We referred to some sites:-


• https://www.google.com/amp/s/www.j2eeonline.com/javapro
gramming/module3/object-oriented-
programmingconclusion.jsp

• https://info.keylimeinteractive.com/the-four-pillars-ofobject-
oriented-programming

• https://www.d.umn.edu/~gshute/softeng/presentations/oopri
nciples.xhtml

14 | P a g e

You might also like