Skip to content

Commit 4e5ece5

Browse files
authored
Update Pointer.md
1 parent 14a2c5b commit 4e5ece5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Pointer.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ C pointers are simple and enjoyable to learn.Some C programming tasks are easier
77
* [Pointer Syntax](#pointer-syntax)
88
* [Get value of things Pointed by Pointers](#get-value-of-things-pointed-by-pointers)
99
* [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)
1014

1115
## Addresses in C
1216
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.
8791
8892
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.
8993
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

Comments
 (0)