You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Pointer.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,10 @@ C pointers are simple and enjoyable to learn.Some C programming tasks are easier
7
7
*[Pointer Syntax](#pointer-syntax)
8
8
*[Get value of things Pointed by Pointers](#get-value-of-things-pointed-by-pointers)
9
9
*[Changing value Pointed by Pointers](#changing-value-pointed-by-pointers)
10
+
*[Some special Pounters](#some-special-pointers)
11
+
-[Wild Pointer](#wild-pointer)
12
+
-[Null Pointer](#null-pointer)
13
+
-[Void Pointer](#void-pointer)
10
14
11
15
## Addresses in C
12
16
Before we get to the definition of pointers, let us understand what happens when we write the following code:
@@ -87,3 +91,31 @@ We have assigned the address of c to the pc pointer.
87
91
88
92
Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is the same, c will be equal to 1.
89
93
94
+
## Some special Pointers
95
+
### Wild Pointer
96
+
```c
97
+
char *alphabetAddress; /* uninitialised or wild pointer */
98
+
char alphabet = "a";
99
+
alphabetAddress = &alphabet; /* now, not a wild pointer */
100
+
```
101
+
When we defined our character pointer alphabetAddress, we did not initialize it.
102
+
103
+
Such pointers are known as wild pointers. They store a garbage value (that is, memory address) of a byte that we don't know is reserved or not (remember int digit = 42;, we reserved a memory address when we declared it).
104
+
105
+
Suppose we dereference a wild pointer and assign a value to the memory address it is pointing at. This will lead to unexpected behaviour since we will write data at a memory block that may be free or reserved
106
+
107
+
### NULL Pointer
108
+
To make sure that we do not have a wild pointer, we can initialize a pointer with a NULL value, making it a null pointer.
109
+
```c
110
+
char *alphabetAddress = NULL/* Null pointer */
111
+
```
112
+
A null pointer points at nothing, or at a memory address that users can not access.
113
+
114
+
### Void Pointer
115
+
A void pointer can be used to point at a variable of any data type. It can be reused to point at any data type we want to. It is declared like this:
116
+
```c
117
+
void *pointerVariableName = NULL;
118
+
```
119
+
Since they are very general in nature, they are also known as generic pointers.
120
+
121
+
With their flexibility, void pointers also bring some constraints. Void pointers cannot be dereferenced as any other pointer. Appropriate typecasting is necessary.
0 commit comments