EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Void Pointer in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Void Pointer in C

Void Pointer in C

Definition of C Void Pointer

A void pointer in C is a pointer that does not have any associated data type. A void pointer in C clearly indicates that it is empty and can only capable of holding the addresses of any type. Further, these void pointers with addresses can be typecast into any other type easily. Memory allocation also gets easy with this type of void pointer in C. It makes all these functions flexible for allocating the bytes and memory appropriately. These pointers in C are very helpful in implementing generic functions in C.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

void *pointer_name;

The syntax flow follows in a way that keyword void is the type of pointer followed by the name of the pointer which is being pointed and allocated as an address allocation. The Pointer declaration is performed with the pointer name and the pointer type supporting any data type. Representation of pointer in terms of C is the same as the pointer of character type.

Example:

void *ptra

This example shows that the pointer is expecting a void type of pointer and then it is being pointed by the pointer whose name is given as ptra inclusive of ‘*’ symbol which denotes that a pointer is being declared and will be used in mere future for dereferencing purpose.

How does Void Pointer Work in C?

The pointer concept in C is very useful as it helps in memory allocation and address management. It helps in implementing two types of pointers namely void pointers and generic pointers. Therefore, it is sometimes called a general-purpose pointer. Referencing and Dereferencing plays a vital role in pointer concept as well as in void pointer.

Let’s walk through the working of the void pointer in C which is performed using pointers that are not at all associated with any other data type. It contains any data type which will contain the address of the value. a pointer declared with keyword void is a void pointer in C. As mentioned earlier referencing and dereferencing are some of the methods associated with pointer concept which will be used. Dereferencing comes into picture whenever it is a need to access the stored value in the pointer variable. Also, there is a type of casting value which is used for dereferencing because none of the pointer value is associated with the data types. The compiler also cannot find the type of variable which is pointed by any type of void pointer. One point to keep in mind is void pointer will not support any kind of arithmetic operation. It makes use of indirection operator ‘*’ to serve the entire purpose. But to serve this problem there is a need to typecast the pointer variable as well for dereferencing. The usage of typecasting is needed because there is no presence of datatype associated at the time of declaration of the pointer. In short, the compiler doesn’t have any appropriate source to get an idea of the type of data type declared. So, it performs the typecasting and meets the requirement to give an intimation of the type of data type used by the void pointer at the time of declaration.

Size of the void pointer is the next point of focus as a void pointer in C functions almost the same as character pointer in C which means a representation of Character type of pointer will be the same as a void pointer in C. Also, the size will vary according to the platform being used by the pointer. Memory allocation also works in some format which means void pointer has the beauty of providing an enhancement feature of memory management with calloc () and malloc () functions which ultimately returns the void as return type. Therefore, these functions can be used to allocate the memory of any data type.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

The most important theme line of all the added advantage which pointer has is that it has the power of reusability for void pointers. It can store any type of object and can retrieve any type of object from the defined object using the indirection operator and operator with proper typecasting. Dereferencing operator as part of the pointer can be used for easy access or manipulation of the stored data in the memory location for the pointer pointing the data type and it will have a direct impact on the value of the data type.

Examples of Void Pointer in C

Following are the examples as given below:

Example #1

This program illustrated the void Pointer in C as it is not associated with any data type at the time of declaration as shown in the given output.

Code:

int r = 11;
char m = 'k';
void *p = &r;
p = &m;

Output:

Void Pointer in C-1.1

Example #2

This program is used to illustrate the dereferencing of the void pointer of C where the input is given to variable with the inception operator which is shown with the following example.

Code:

#include<stdio.h>
int main()
{
int o = 18;
void *ptr = &o;
printf("%d", *(int *)ptr);
return 0;
}

Output:

Void Pointer in C-1.2

Example #3

This program illustrates the void pointer in C for representing the size of integer value define with the arithmetic operator which means it manages to support the arithmetic operator in association with a void pointer. Although it needs to follow some of the standards of the GNU then only the compilation allowed will provide the necessary output as shown otherwise it will give a compilation error.

Code:

#include<stdio.h>
int main()
{
int q[3] = {5,9,7};
void *ptr = &q;
ptr = ptr + sizeof(int);
printf("%d", *(int *)ptr);
return 0;
}

Output:

Output-1.3

Example #4

This program is used to illustrate the basic concept of dereferencing with the void pointer in C and its output is as follows.

Code:

#include<stdio.h>
void main()
{
int r=17;
float q=13.8;
char i='c';
void *p;
p=&r;
printf("%d",*((int*)p));
p=&q;
printf("n%f",*((float*)p));
p=&i;
printf("n%c",*((char*)p));
}

Output:

 Output-1.4

Conclusion

void pointer in C is used to mitigate the problem of pointers pointing to each other with a different set of values and data types. There is also a reduction in explicit typecasting. Also, it supports the generic pointer type which makes it as a generic-purpose compiler.

Recommended Articles

This is a guide to Void Pointer in C. Here we also discuss the definition and how does void pointer work in c? along with different examples and code implementation. You may also have a look at the following articles to learn more –

  1. Constants in C
  2. Double Pointer in C
  3. Function Pointer in C
  4. Null pointer in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More