Skip to content

Commit

Permalink
Day 11: Pointers in Array in C++ (#282)
Browse files Browse the repository at this point in the history
* changes in day-09

* changes in day 9

* changes in day 11

* day 11 pointers

---------

Co-authored-by: Shubhadip Bhowmik <[email protected]>
  • Loading branch information
shaziaakhan and subhadipbhowmik authored Jun 13, 2024
1 parent 3ddb48b commit f308ed8
Showing 1 changed file with 1 addition and 44 deletions.
45 changes: 1 addition & 44 deletions docs/day-11/pointers-in-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,47 +254,4 @@ increment(p);
```
After the increment() function is called, the value of the variable x will be incremented to 6.
Here is an example of a complete program that passes a pointer to a function:
```cpp
#include <iostream>
using namespace std;
void increment(int *p) {
*p += 1;
}
int main() {
int x = 5;
int *p = &x;
increment(p);
cout << x << endl; // Prints 6
return 0;
}
```

## How to Return a Pointer from a Function in C++?

To return a pointer from a function in C++, you need to declare the return type of the function as a pointer to the type of data that you want to return. For example, if you want to return a pointer to an integer, you would declare the return type of the function as int*.

Here is an example of a function that returns a pointer to an integer:

```cpp
int* getIntPtr() {
int* pInt = new int(10);
return pInt;
}
```

This function allocates a new integer on the heap and returns a pointer to it. The caller of the function is then responsible for deleting the integer when it is no longer needed.
Here is an example of how to call the getIntPtr() function:

```cpp
int* pInt = getIntPtr();
cout << *pInt << endl; // Prints "10"
delete pInt;
```
Here is an example of a complete program that passes a pointer to a function:

0 comments on commit f308ed8

Please sign in to comment.