Skip to content

Commit

Permalink
Update c-5.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-dhangar authored Nov 10, 2024
1 parent e623d2d commit 124d269
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions docs/languages/C/c-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ sidebar_label: "Pointers In C"
Hey there! In this article, we will discuss C **pointers** in detail, their types, uses, advantages, and disadvantages with examples.

- In C, the **pointers** are used to store the address of another variable and can also be used to access and manipulate the variable's data stored at that location.
- **Pointers** can also store the addresses of functions, array elements and even the addresses of another pointers
- With **pointers**,we can access and modify the data located in the memory, pass the data efficiently between the functions, and create dynamic data structures like linked lists, trees, and graphs.
- **Pointers** can also store the addresses of functions, array elements and even the addresses of another pointer
- With **pointers**, we can access and modify the data located in the memory, pass the data efficiently between the functions, and create dynamic data structures like linked lists, trees, and graphs.

<Ads />

## 1. Pointer Declaration :

Expand All @@ -35,9 +37,11 @@ int *ptr;

The pointer declared here will point to some random memory address as it is not initialized.

<Ads />

## 2. Pointer Initialization :

Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the **( &: ampersand )addressof** operator to get the memory address of a variable and then store it in the pointer variable.
Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the **( &: ampersand ) address of** operator to get the memory address of a variable and then store it in the pointer variable.

### Example :

Expand Down Expand Up @@ -152,6 +156,8 @@ Size of char pointer : 8

---

<Ads />

## 5. Types of Pointers in C :

Based on the type of variable stored in the memory location pointed by the pointer, the
Expand Down Expand Up @@ -241,6 +247,8 @@ printf("Roll = %d\n",(*ptr).roll); // OUTPUT :Roll = 1
---
<Ads />
### 4. **Function Pointer**
**Function pointers point to the functions**. They are different from the rest of the pointers in the sense that **instead of pointing to the data, they point to the code**
Expand All @@ -263,7 +271,7 @@ void main()

- Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.
- Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
- A function's name can also be used to get functions' address.
- A function's name can also be used to get function's address.
**Example**

```c
Expand All @@ -282,6 +290,8 @@ void main()
---
<Ads />
### 5. **Double Pointers**
In C language, we can define **a pointer that stores the memory address of another pointer**. Such pointers are called **double-pointers** or **pointers-to-pointer**. Instead of pointing to a data value, they point to another pointer.
Expand All @@ -308,6 +318,8 @@ int **d_ptr = &ptr; // pointer to a pointer declared & initialized

---

<Ads />

### 6. **Void Pointer**
The **Void pointers** in C are the pointers of type void. It means that they do not have any associated data type. They are also called **generic pointers** as they can point to any type and can be typecasted to any type.

Expand All @@ -317,7 +329,7 @@ int **d_ptr = &ptr; // pointer to a pointer declared & initialized
void * pointer_name;
```

**Some uses of Void Pointers :**
**Some uses of Void Pointers:**

- **malloc()** and **calloc()** return **void \* type** and this allows these functions to be used to allocate memory of any data type (just because of void \*).
- void pointers used along with Function pointers of type void (\*)(void) point to the functions that take any arguments and return any value.
Expand All @@ -336,7 +348,7 @@ data_type *pointer_name = NULL;
- It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
- The NULL pointer is a constant with a value of "0" defined in several standard libraries.

**Some uses of NULL Pointers :**
**Some uses of NULL Pointers:**

- We initialize a pointer variable when that pointer variable hasn't been assigned any valid memory address yet.
- We check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer-related code, e.g., dereference a pointer variable only if it's not NULL.
Expand All @@ -345,6 +357,8 @@ data_type *pointer_name = NULL;

---

<Ads />

## 6. Other important pointer types :
### 1. **Dangling Pointer**
**A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer**. Such a situation can lead to unexpected behavior in the program and also serve as a source of bugs in C programs.
Expand Down Expand Up @@ -428,17 +442,21 @@ data_type *pointer_name = NULL;
---
## 7. Pointer Arithmatic
<Ads />
## 7. Pointer Arithmetic
**Pointer Arithmetic** is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. These operations are:
Hence, there are only a few operations that are allowed to be performed on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. These operations are:
- Increment and Decrement of a Pointer
- Addition and Subtraction of Integer to Pointer
- Subtraction of Pointers
- Comparison of Pointers
<Ads />
### a. Increment and Decrement of a Pointer
The "++" and "--" are used as the increment and decrement operators in C. They are unary operators, used in prefix or postfix manner with numeric variable operands, and they increment or decrement the value of the variable by one.
The "++" and "--" are used as the increment and decrement operators in C. They are unary operators, used in prefix or postfix manner with numeric variable operands, and they increment or decrease the value of the variable by one.
Assume that an integer variable "a" is created at address 1000 in the memory, with 24 as its value. Then, "a++" makes the value of "a" as 25.
Expand All @@ -447,7 +465,7 @@ int a = 24; // created at address 1000
a++; // a becomes 25
```
Now, let's see what happens if we declare "p" as pointer to "a" and increment "p" by 1 (with "p++")? Assume that the address of "p" itself is 2000.
Now, let's see what happens if we declare "p" as a pointer to "a" and increment "p" by 1 (with "p++")? Assume that the address of "p" itself is 2000.

```c
int a = 24; // created at address 1000
Expand All @@ -462,6 +480,8 @@ Since the variable "p" stores 1000 (the address of "a"), we expect it to become

The is why because, if the address of "a" is 1000, then it occupies 4 bytes: 1000, 1001, 1002 and 1003. Hence, the next integer can be put only in 1004 and not before it. Hence "p" (the pointer to "a") becomes 1004 when incremented.

<Ads />

#### Example of Incrementing a Pointer
```c
#include <stdio.h>
Expand All @@ -486,7 +506,7 @@ Value of y after increment: 6422040
You can see that the value has increased by 4. Similarly, the "--" operator decrements the value by the size of the data type.

#### Example of Decrementing a Pointer
Now, let us change the types of "x" and "y" to "double" and "float" and see the effect of decrement operator.
Now, let us change the types of "x" and "y" to "double" and "float" and see the effect of the decrement operator.
```c
#include <stdio.h>
void main(){
Expand All @@ -507,6 +527,9 @@ Value of y before decrement: 6422032
Value of y after decrement: 6422024
```
---

<Ads />

#### Example of Traversing an Array by Incrementing Pointer

When an array is declared, the elements are stored in adjacent memory locations. In case of "int" array, each array subscript is placed apart by 4 bytes.
Expand Down Expand Up @@ -545,6 +568,9 @@ Address of subscript 7 = 6422012 Value = 80
Address of subscript 8 = 6422016 Value = 90
Address of subscript 9 = 6422020 Value = 100
```

<Ads />

### b. Addition and Subtraction of Integer to Pointer
An integer value can be added and subtracted to a pointer. When an integer is added to a pointer, the pointer points to the next memory address. Similarly, when an integer is subtracted from a pointer, the pointer points to the previous memory location.

Expand Down Expand Up @@ -604,12 +630,15 @@ void main() {
Value at ptrArr: 89
Value at ptrArr after adding 2: 45
```

<Ads />
]
### c. Subtraction of Pointers
The "+" and "−" operators are used with regular numeric operands. However, when you use these operators with pointers, they behave in a little different way.

Since pointers are fairly large integers (especially in modern 64-bit systems), addition of two pointers is meaningless. When we add a 1 to a pointer, it points to the next location where an integer may be stored. Obviously, when we add a pointer (itself a large integer), the location it points may not be in the memory layout.

However, subtraction of two pointers is realistic. It returns the number of data types that can fit in the two pointers.
However, the subtraction of two pointers is realistic. It returns the number of data types that can fit in the two pointers.

#### Example of Subtracting Two Pointers
Let us take the array in the previous example and perform the subtraction of pointers of a[0] and a[9]
Expand All @@ -632,10 +661,12 @@ void main(){
Add of a[0]: 140729162482768 add of a[9]: 140729162482804
Subtraction of two pointers: 9
```
It can be seen that the numerical difference between the two integers is 36; it suggests that the subtraction is 9, because it can accommodate 9 integers between the two pointers.
It can be seen that the numerical difference between the two integers is 36; it suggests that the subtraction is 9 because it can accommodate 9 integers between the two pointers.

### d.Comparison of Pointers
Pointers can be compared by using **relational operators** such as "==", "<", and ">". If "p1" and "p2" point to variables that are related to each other (such as elements of the same array), then "p1" and "p2" can be meaningfully compared.
Pointers can be compared by using **relational operators** such as `==`, `<`, and `>`. If "p1" and "p2" point to variables that are related to each other (such as elements of the same array), then "p1" and "p2" can be meaningfully compared.

<Ads />

#### Example of Comparing Pointers
In the following example, we are declaring two pointers and initializing them with the first and last elements of the array respectively. We will keep incrementing the first variable pointer as long as the address to which it points is either less than or equal to the address of the last element of the array, which is "&var[MAX − 1]" (i.e., the second pointer).
Expand Down Expand Up @@ -672,6 +703,7 @@ Address of var[2] = 0x7ffe71014994
Value of var[2] = 200
```

<Ads />

## 8. Applications of pointers in C :
The following are some major applications of pointers in the C programming language:
Expand Down Expand Up @@ -758,7 +790,10 @@ The following are some major applications of pointers in the C programming langu
printf("%d %f\n", sq, sq_root);
return 0;
}
```
```
<Ads />
### 4. **For dynamic memory Allocation**
We can use pointers to dynamically allocate memory i.e Dynamic memory allocation.But dynamic memory doesn't get deleted automatically, we need to delete it for a good practice after using it.
**Example**
Expand Down Expand Up @@ -790,6 +825,8 @@ pointer_type *array_name [array_size];
- **array_name**: Name of the array of pointers.
- **array_size**: Size of the array of pointers.

<Ads />

### a. An Array of Pointers to Integers
#### Syntax
```c
Expand Down Expand Up @@ -852,6 +889,8 @@ Value of names[1] = Srijita Mondal
Value of names[2] = Monami Das
Value of names[3] = Poulami Bhandari
```
<Ads />

### c. An Array of Pointers to Structures
When you have a list of structures and want to manage it using a pointer. You can declare an array of structures to access and manipulate the list of structures.
#### Example
Expand Down Expand Up @@ -898,7 +937,10 @@ Title: Book 3, Price: 102.00

---

<Ads />

## 10. Advantages & Disadvantages of Pointers in C :

### Advantages of Pointers in C
Following are the major advantages of pointers in C:

Expand All @@ -909,6 +951,7 @@ Following are the major advantages of pointers in C:
- Pointers reduce the length of the program and its execution time as well.

### Disadvantages of Pointers in C

Pointers are vulnerable to errors and have following disadvantages:

- Memory corruption can occur if an incorrect value is provided to pointers.
Expand Down

0 comments on commit 124d269

Please sign in to comment.