Add void pointer to index.md (#22248)
* Update index.md I added void pointers, which can be very useful in C programming, especially when dealing with unknown data types but had not been included. * fixed formatting issues, removed attributions, moved placement to more appropriate location * adjusted section header size
This commit is contained in:
committed by
Christopher McCormack
parent
e759361716
commit
e73ffaf2fe
@ -166,7 +166,7 @@ This starts by taking a string (something you'll learn about when you get into a
|
|||||||
### Const Qualifer
|
### Const Qualifer
|
||||||
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change value of const variable by using pointer ).
|
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change value of const variable by using pointer ).
|
||||||
|
|
||||||
# Pointer to variable
|
### Pointer to variable
|
||||||
We can change the value of ptr and we can also change the value of object ptr pointing to.
|
We can change the value of ptr and we can also change the value of object ptr pointing to.
|
||||||
Following code fragment explains pointer to variable
|
Following code fragment explains pointer to variable
|
||||||
```c
|
```c
|
||||||
@ -189,7 +189,7 @@ int main(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
# Pointer to constant
|
### Pointer to constant
|
||||||
We can change pointer to point to any other integer variable, but cannot change value of object (entity) pointed using pointer ptr.
|
We can change pointer to point to any other integer variable, but cannot change value of object (entity) pointed using pointer ptr.
|
||||||
```c
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -209,7 +209,7 @@ int main(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
# Constant pointer to variable
|
### Constant pointer to variable
|
||||||
In this we can change the value of the variable the pointer is pointing to. But we can't change the pointer to point to
|
In this we can change the value of the variable the pointer is pointing to. But we can't change the pointer to point to
|
||||||
another variable.
|
another variable.
|
||||||
```c
|
```c
|
||||||
@ -229,7 +229,7 @@ int main(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
# constant pointer to constant
|
### Constant pointer to constant
|
||||||
Above declaration is constant pointer to constant variable which means we cannot change value pointed by pointer as well as we cannot point the pointer to other variable.
|
Above declaration is constant pointer to constant variable which means we cannot change value pointed by pointer as well as we cannot point the pointer to other variable.
|
||||||
```c
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -249,6 +249,54 @@ int main(void)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Void Pointer
|
||||||
|
A void pointer is a pointer variable declared using the reserved word in C ‘void’.
|
||||||
|
Lets illustrate this with a void pointer declaration below:
|
||||||
|
```C
|
||||||
|
void *ptr;
|
||||||
|
```
|
||||||
|
A pointer variable with the keyword `void`is a general purpose pointer variable.
|
||||||
|
The pointer can hold an address of any variable of any data type (`int`, `char`...etc).
|
||||||
|
|
||||||
|
As illustrated earlier on, the * operator serves its own purpose.
|
||||||
|
But in the case of a void pointer we need to typecast the pointer variable to dereference it mainly because a void pointer has no specific data type associated with it.
|
||||||
|
There is no other way the compiler can tell what type of data is pointed to by the void pointer.
|
||||||
|
So to take the data pointed to by a void pointer we typecast it with the correct type of the data that is held inside the void pointer's location.
|
||||||
|
|
||||||
|
Below is an example to illustrate how a void pointer coild be used in a program:
|
||||||
|
|
||||||
|
```C
|
||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int a = 10;
|
||||||
|
float b = 35.75;
|
||||||
|
void *ptr; // Declaration of a void pointer
|
||||||
|
ptr = &a; // Assigning address of integer to void pointer.
|
||||||
|
printf("The value of integer variable is = %d",*( (int*) ptr) );// (int*)ptr - is ype typecasting, to point to an int type. Where as *((int*)ptr) dereferences the typecasted void pointer variable.
|
||||||
|
}
|
||||||
|
```
|
||||||
|
The output becomes
|
||||||
|
```output
|
||||||
|
The value of integer variable is = 10
|
||||||
|
```
|
||||||
|
|
||||||
|
A void pointer can be useful if the programmer is not sure about the data type of data inputted by the end user.
|
||||||
|
In such a case the programmer can use a void pointer to point to the location of the unknown data type.
|
||||||
|
The program can be set in such a way to ask the user to inform the type of data and type casting can be performed according to the information inputted by the user.
|
||||||
|
|
||||||
|
Another important point you should keep in mind about void pointers is that pointer arithmetic can not be performed in a void pointer.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```C
|
||||||
|
void *ptr;
|
||||||
|
int a;
|
||||||
|
ptr=&a;
|
||||||
|
ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable.
|
||||||
|
```
|
||||||
|
Credits: <http://www.circuitstoday.com/void-pointers-in-c>
|
||||||
|
|
||||||
|
|
||||||
# Before you go on...
|
# Before you go on...
|
||||||
## A review
|
## A review
|
||||||
* Pointers are variables, but instead of storing a value, they store a memory location.
|
* Pointers are variables, but instead of storing a value, they store a memory location.
|
||||||
@ -282,4 +330,3 @@ Most of the time, pointer and array accesses can be treated as acting the same,
|
|||||||
p++; /*Legal*/
|
p++; /*Legal*/
|
||||||
a++; /*illegal*/
|
a++; /*illegal*/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user