Skip to content

Commit a517d51

Browse files
Add files via upload
1 parent ff5ece1 commit a517d51

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

24_pointerswithFunctions.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
void printValue(int* p){
4+
cout<<"Address stored by the pointer is: "<<p<<endl;
5+
cout<<"Value at the address stored by the pointer is: "<<*p<<endl;
6+
}
7+
void update(int* p){
8+
p++;
9+
}
10+
int main(){
11+
// Pointers can be passed in the functions as well.
12+
int n = 3;
13+
int* ptr = &n;
14+
15+
printValue(ptr);//this print function is going to print the address and value of the pointer
16+
cout<<endl;
17+
// Now what we are going to do is going to create a function update() which is going to update the value at address stored by the pointer and lets see it will going to do it or not
18+
cout<<"Before change,address stored by pointer is: "<< ptr<<endl;
19+
update(ptr);
20+
cout<<"After change,address stored by the pointer is: "<< ptr <<endl;// evne after updation , address is not gonna change because here the pointer is pass by value which means a copy of the pointer is being created in the update function and the address is changed within the scope of it and is not getting reflected in the main(); Then whats the solution: either print the address inside the update function or use reference variables.
21+
22+
23+
return 0;
24+
}

24_pointerswithFunctions.exe

44.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)