Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the content for day 10 #540

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
91 changes: 91 additions & 0 deletions docs/day-10/Advanced_Pointer_Concepts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
sidebar_position: 3
title: "Advance Pointer Concept in C++"
description: "In this tutorial, we will learn about some advance concepts of Pointers in C++ programming with the help of examples."
sidebar_label: "Advance Pointer Concept"
slug: Advanced_Pointer_Concept
---
# Advanced Pointer Concepts in C++

## 1. Pointer to Pointer (Double Pointers)

A pointer to a pointer (double pointer) holds the address of another pointer, which in turn holds the address of a variable. This allows for multiple levels of indirection.

**Usage**:
- Useful in dynamic memory allocation for multidimensional arrays.
- Used in functions to modify the original pointer.

**Example**:
```cpp
#include <iostream>

int main() {
int a = 10;
int* ptr = &a; // Pointer to an integer
int** ptr2 = &ptr; // Pointer to a pointer

std::cout << "Value of a: " << a << std::endl; // Outputs 10
std::cout << "Value pointed to by ptr: " << *ptr << std::endl; // Outputs 10
std::cout << "Value pointed to by ptr2: " << **ptr2 << std::endl; // Outputs 10

return 0;
}
```


## 2. Const Pointers and Pointers to Const

## Const Pointers
A const pointer is a pointer whose address cannot be changed after it has been initialized. This means the pointer will always point to the same memory location.

**Syntax** :
int* const ptr = &a;

**Usage**:

Use when you want the pointer to always point to the same address.
**Example:**

```cpp
Copy code
int a = 10;
int b = 20;
int* const ptr = &a; // ptr is a const pointer to an integer
// ptr = &b; // Error: cannot change the address ptr points to
*ptr = 15; // Allowed: can change the value at the address
```
## Pointers to Const
A pointer to const is a pointer that points to a constant value. This means the value being pointed to cannot be changed through the pointer.


**Syntax**:
const int* ptr = &a;

**Usage**:

Use when the data being pointed to should not be modified through the pointer.
**Example**:

```cpp
Copy code
int a = 10;
const int* ptr = &a; // ptr is a pointer to a const integer
// *ptr = 15; // Error: cannot change the value at the address
ptr = &b; // Allowed: can change the address ptr points to
```

## Const Pointer to Const
Combines both concepts: the pointer itself cannot change, and the value it points to cannot be changed.

**Syntax**:
const int* const ptr = &a;

**Example**:

```cpp
Copy code
const int a = 10;
const int* const ptr = &a; // ptr is a const pointer to a const integer
// *ptr = 15; // Error: cannot change the value at the address
// ptr = &b; // Error: cannot change the address ptr points to
```
174 changes: 174 additions & 0 deletions docs/day-10/Types_Of_Pointer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
sidebar_position: 2
title: "Types Of pointers"
description: "In this tutorial, we will learn about different types of Pointers in C++ programming with the help of examples."
sidebar_label: "Types Of pointers"
slug: Types_Of_Pointer
---

## Initialization of Pointers in C++?
Initializing pointers in C++ is an essential step to ensure that they point to a valid memory location before use.

### Example:
```cpp
int a = 10;
int* ptr = &a; // ptr now holds the address of variable a
```
## Dereferencing of Pointers in C++?
Dereferencing a pointer means accessing the value stored at the memory address the pointer holds. This is done using the asterisk * operator:


### Example:
```cpp
int a = 10;
int* ptr = &a;
cout << *ptr; // Outputs the value of a, which is 10

```


## Null Pointers in C++?
A pointer that is not initialized or that you explicitly want to point to nothing can be set to nullptr (in C++11 and later) or NULL:


### Example:
```cpp
int* ptr = nullptr;
```

## Pointer Arithmetic in C++?
Pointers can be incremented or decremented to point to other memory locations. This is commonly used with arrays.


### Example:
```cpp
int arr[] = {10, 20, 30};
int* ptr = arr; // Points to the first element
ptr++; // Now points to the second element
cout << *ptr; // Outputs 20

```


## Pointers and Arrays in C++?
The name of an array acts as a pointer to the first element of the array.


### Example:
```cpp
int arr[] = {10, 20, 30};
int* ptr = arr; // Equivalent to int* ptr = &arr[0];
cout << *(ptr + 1); // Outputs 20

```


## Pointers and Functions in C++?
Pointers can be used to pass variables to functions by reference, allowing the function to modify the original variable.


### Example:
```cpp
void increment(int* ptr) {
(*ptr)++;
}

int main() {
int a = 10;
increment(&a);
cout << a; // Outputs 11
return 0;
}

```

## Dynamic Memory Allocation in C++?
Pointers are essential for dynamic memory allocation using new and delete.


### Example:
```cpp
int* ptr = new int; // Allocate memory for an integer
*ptr = 20;
delete ptr; // Free the allocated memory

int* arr = new int[10]; // Allocate memory for an array of 10 integers
delete[] arr; // Free the allocated memory

```


## Pointers to Pointers in C++?
A pointer can point to another pointer, allowing for multi-level indirection.


### Example:
```cpp
int a = 10;
int* ptr = &a;
int** ptr2 = &ptr;
cout << **ptr2; // Outputs 10
```


## Function Pointers in C++?
Function pointers store the address of a function and can be used to call functions dynamically.


### Example:
```cpp

void myFunction(int a) {
cout << a;
}

int main() {
void (*funcPtr)(int) = myFunction;
funcPtr(10); // Calls myFunction(10)
return 0;
}

```


## Smart Pointers in C++?
Modern C++ provides smart pointers (unique_ptr, shared_ptr, weak_ptr) to manage dynamically allocated memory automatically.


### Example:
```cpp
#include <memory>

std::unique_ptr<int> ptr = std::make_unique<int>(10);
std::shared_ptr<int> sharedPtr = std::make_shared<int>(20);
std::weak_ptr<int> weakPtr = sharedPtr;

```

# Summary Table

| Pointer Type | Description | Usage |
|-----------------------------|---------------------------------------|--------------------------------------------|
| Null Pointer | Points to `nullptr` | Indicates no valid memory location |
| Void Pointer | Holds address of any data type | Generic pointer usage |
| Wild Pointer | Uninitialized pointer | Avoid using, can cause undefined behavior |
| Dangling Pointer | Points to deallocated memory | Set to `nullptr` after deleting memory |
| Constant Pointer | Cannot change the address it holds | Always point to the same address |
| Pointer to Constant | Points to a constant value | Data pointed to should not be modified |
| Constant Pointer to Const | Both a constant pointer and points to a constant | Neither address nor value should be changed |
| Function Pointer | Points to a function | Callbacks and function tables |
| Null Pointer Constant | Defined by the macro `NULL` | Use `nullptr` in modern C++ |
| Smart Pointers | Provide automatic memory management | Safer and more efficient memory management |



## Key Points to Remember
- Always initialize pointers.
- Use nullptr instead of NULL in modern C++.
- Be cautious with pointer arithmetic and avoid accessing out-of-bounds memory.
- Use delete to free dynamically allocated memory to avoid memory leaks.
- Prefer smart pointers to raw pointers for automatic memory management.




7 changes: 7 additions & 0 deletions docs/day-10/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"label": "Day 10",
"position": 11,
"link": {
"type": "generated-index"
}
}
78 changes: 78 additions & 0 deletions docs/day-10/pointer-in-cpp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
sidebar_position: 1
title: "Pointers in C++"
description: " A pointer is a variable that stores the memory address of another variable."
sidebar_label: "Pointers"
slug: pointer-in-cpp
---

## What is Pointer in C++?
In C++, a pointer is a variable that holds the memory address of another variable. Pointers are powerful and versatile tools that allow for efficient memory management and dynamic data structures


## How to Declare Pointers in C++?
Declaring pointers in C++ involves specifying the type of the data the pointer will point to, followed by an asterisk *, and then the pointer's name. Here’s how you can declare pointers of various types:

### Example:
```cpp
int* ptr;
```

## Initialization of Pointers in C++?
Initializing pointers in C++ is an essential step to ensure that they point to a valid memory location before use.

### Example:
```cpp
int a = 10;
int* ptr = &a; // ptr now holds the address of variable a
```

## How to Use Pointers in C++?
Using pointers in C++ involves various operations like declaring, initializing, dereferencing, performing pointer arithmetic, and managing dynamic memory.

### Example

```cpp
int arr[] = {10, 20, 30};
int* ptr = arr; // Points to the first element of the array
ptr++; // Now points to the second element
std::cout << *ptr; // Outputs 20

```

## What are the Advantages of Pointers in C++?
- Dynamic Memory Allocation: Pointers allow the creation of dynamic data structures like linked lists, trees, and graphs that can grow and shrink at runtime.
- Efficient Array and String Manipulation: Pointers enable efficient iteration and manipulation of arrays and strings.
- Function Argument Passing: Pointers allow functions to modify the original variables by passing their addresses, supporting pass-by-reference.
- Memory Management: Pointers provide control over system memory, enabling efficient memory usage and management.
- Interfacing with System-Level Programming: Pointers are essential for low-level programming, interacting with hardware, and writing performance-critical code.
- Simplifying Complex Data Structures: Pointers facilitate the implementation of complex data structures like linked lists, trees, and graphs.
- Efficient Resource Management: With smart pointers, C++ can manage resources automatically, reducing the risk of memory leaks and dangling pointers.

## Best Practices for Using Pointers in C++:
- Always Initialize Pointers: Ensure pointers are initialized before use to avoid undefined behavior.
- Use nullptr Instead of NULL: In C++11 and later, prefer nullptr over NULL for better type safety.
- Avoid Dangling Pointers: Ensure pointers do not reference memory that has been deallocated or is out of scope.
- Use Smart Pointers: Utilize smart pointers like std::unique_ptr, std::shared_ptr, and std::weak_ptr for automatic memory management and to prevent memory leaks.
- Be Careful with Pointer Arithmetic: Avoid pointer arithmetic unless necessary to prevent out-of-bounds access and undefined behavior.
- Free Allocated Memory: Always free dynamically allocated memory using delete or delete[] to prevent memory leaks.


## Examples of Pointers in C++

```cpp
#include <iostream>

int main() {
int a = 10; // Declare an integer variable
int* ptr = &a; // Declare a pointer and initialize it with the address of 'a'

std::cout << "Value of a: " << a << std::endl;
std::cout << "Address of a: " << &a << std::endl;
std::cout << "Value of ptr: " << ptr << std::endl; // Address of 'a'
std::cout << "Value pointed to by ptr: " << *ptr << std::endl; // Value of 'a'

return 0;
}

```