C Program Void pointer holding Integer Addresses & Character Type

CWC
6 Min Read
C Program Void pointer holding Integer addresses & Character Type

How Void Pointer Holding Integer Addresses and Character Type. We have already written an article on void pointers and how they are used in the C programming language. In that article, we have mentioned that the void pointer holds the address of any type of variable. The first question that comes to our mind is “how is it possible to hold the address of an integer variable”?

C Program Void pointer holding Integer addresses & Character Type

Here I am going to explain it through an example. Suppose we have a variable of int type whose value is 10, if we print its address using void pointer then it will print the address of variable “10”. This is because the void pointer is capable of holding the address of any type of variable.

Let’s say we have a function that prints the value of the variable “int x”. So, when the function is called, the variable “x” will be automatically allocated in memory.


void printValueOfInteger(int *x)
{
printf("The value of the variable is %d",*x);
}
int main()
{
int x=10;
printValueOfInteger(&x);
return 0;
}

After running the code, the output will be as follows:

The value of the variable is 10

Suppose we have a function that prints the value of the variable “char y”. So, when the function is called, the variable “y” will be automatically allocated in memory.


void printValueOfChar(char *y)
{
printf("The value of the variable is %c",*y);
}
int main()
{
char y=’a’;
printValueOfChar(&y);
return 0;
}

The output will be: The value of the variable is a

What is the output of the following program?


#include

int main()
{
int b = 25; void *p = &b;
int *j = (int*)p; char *ch = (char*)p;
printf("%d, %c\n", *j, *ch); return 0;
}

The output will be: 25, A

The pointer that is void pointer is a general pointer type. It can store addresses of any type of data. It is used to determine the type of data used by an address is not known.

Pointer isn’t known at time but in the later it could be identified. In the example code, we use an empty pointer holding addresses of an integer as well as characters of the type.

Character Type and Void Pointers

Character Type

The character type is a fundamental type of the C programming language. It represents the alphabetical characters (like A, B, C, etc.).

Void pointer

Void pointer is a special type of pointer that points to the memory location that holds the data. The address of the void pointer can be obtained by using the void pointer type.

Example:


int x = 10;
void *pointer;
pointer = &x;

In the above example, the address of x is pointed by the pointer variable. You can also write the code as follows:


int x = 10;
void *pointer;
pointer = &x;
cout << pointer << endl;

This will print the address of x in the output window.

Now, let’s see an example that will illustrate the use of void pointers.

Example:


int main()
{
int x = 10;
char c = 'c';
int y = 5;
int z = 7;
void *ptr1, *ptr2, *ptr3;
ptr1 = &x;
ptr2 = &y;
ptr3 = &z;
// print the address of ptr1
printf("Address of ptr1: %p\n", ptr1);
// print the address of ptr2
printf("Address of ptr2: %p\n", ptr2);
// print the address of ptr3
printf("Address of ptr3: %p\n", ptr3);
return 0;
}

Output:

Address of ptr1: 0x7fff6ff7d0e8

Address of ptr2: 0x7fff6ff7d0e0

Address of ptr3: 0x7fff6ff7d0df

In the above example, when the void pointer is assigned the address of x, the address of x is printed in the output window.

Character type and void pointer are two important types of the C programming language.

Conclusion:

In this post, I have discussed the character type and void pointer in brief. I hope you have understood the use of character type and void pointer in C programming language. So, now you know that the void pointer can hold the address of any type of variables. Now, the next question is “What is the use of void pointer in C programming language?”.

Well, it is the best way to pass the data to a function. Suppose we have a function that prints the value of the variable “int x”. The value of the variable will be automatically allocated in memory.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version