Professional Documents
Culture Documents
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
Miss.Swapnali Teli
Prof. Mr.S.M.Mayekar
1|Page
Pointer
CERTIFICATE
This is to certify that,
Ms. DISHA DIPAK NAIK. Roll no. 26
Ms. GAURI VISHNU MOCHEMADKAR. Roll no. 30
Seal of
Institution
2|Page
Pointer
INDEX
1. Abstract 4
2. introduction 5
4. Application of pointer 7
5. Advantages of pointer 8
6. Usage of pointer 9
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
Syntax:
datatype *var_name;
int *ptr; //ptr can point to an address
Fig.1
5|Page
Pointer
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.
• call-by-value
7|Page
Pointer
• 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.
• 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
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
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
13 | P a g e
Pointer
Reference
• 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