SoFunction
Updated on 2025-04-12

Deeply understand void in C language*

1. The type arbitrary of void*

void* is a general pointer type. It can point to any type of data. For example, it can point to an integer, a float, a character, or a structure, etc. In C language, when you usevoid*When pointering, you don't need to specify the type of data it will point to at compile time. This makesvoid*It is very useful in some scenarios where universal pointers are required, such as the memory allocation function malloc is returned.void*A pointer of type. Because the malloc function does not know what type of data the user will allocate to store, it returns avoid*Pointer, which users can convert to a specific type of pointer according to their needs.
For example:

void* ptr = malloc(10 * sizeof(int));
int* intPtr = (int*)ptr;  // Convert void* pointer to int* pointer

In this example,void*Pointer ptr can point to an allocated memory area and then convert it to an int* pointer by type conversion, which is used to store an array of integers.

2. Compiler type check for void*

No type checking is done during compilation (for void* itself). The compiler will not be corrected at compile timevoid*The pointer itself performs type checking. becausevoid*A pointer indicating "unknown type", the compiler cannot know the data type it actually points to. So, when you operate on void pointers (such as assignments, etc.), the compiler will not check if the data type it points to is correct. For example, you can assign a pointer to an integer to a void pointer, or assign a pointer to a character to a void* pointer, and the compiler will not report an error.

int a = 10;
char b = 'k';
void* vp1 = &a;
void* vp2 = &b;

In this example, vp1 and vp2 are void* pointers, pointing to different types of variables a and b, respectively. The compiler will not type check for this assignment operation.

3. Explicit type conversion is required

When you want to usevoid*When a pointer accesses a certain value, it is usually necessary to convert it to a pointer of a specific type, and then access the value through the converted pointer. When converting, you need to specify the target type explicitly, and the compiler will check the converted pointer type. For example, if you want to passvoid* Pointer accesses the value of an integer,You need to convert it to int*Pointer.
For example:

void* vp = malloc(sizeof(int));
*(int*)vp = 20;  // First convert void* to int*, and then assign values ​​through int* pointer

In this example, vp is avoid*Pointer, pointing to allocated memory. Before assigning a value to this piece of memory, it is necessary to convert it toint*pointer.If the target type of the converted does not match the actual stored data type, it may result in a runtime error.For example, if this piece of memory actually stores a floating point number and you convert it into an int* pointer and access it, you may get an incorrect result or throw a program exception. The compiler checks whether the syntax is correct (such as whether there is a suitable type conversion operation) when converting, but the correctness of type conversion (i.e. whether it conforms to the actual logic of the program) mainly depends on the correct use of the programmer.

Occupied bytes

1. 32-bit system
In a 32-bit system,void*Pointers usually occupy 4 bytes. This is because the memory address space in a 32-bit system is 2 to the power of 32 (i.e. 4GB), and a memory address can be represented by 4 bytes (32-bit). For example, on a 32-bit Windows system or a 32-bit Linux system, whether it is a void pointer or other type of pointer (such as int, char*, etc.), they all occupy 4 bytes. These 4 bytes store a memory address, which can point to any location in the process address space.

2. 64-bit system
In a 64-bit system,void*Pointers usually occupy 8 bytes. A 64-bit system has a larger memory address space, which can theoretically reach 2 bytes to the power of 64. Therefore, 8 bytes (64 bits) are needed to represent a complete memory address. On 64-bit systems, whether it isvoid*Pointers or other types of pointers, their size is 8 bytes. This allows 64-bit systems to access larger memory space, support larger data processing and more complex program operation.

4. Summary

Through our introduction above, we foundvoid*It is often used in C language, and it mainly has the following solutions:

  • The first solution is to act as a field of the structure, so that we can represent the corresponding type field
  • The parameters of the function, or the return value, but I think this is the best to use less because the corresponding interface is not clear enough
  • Various memory-related functions are actually a very wonderful thing, because at the operating system level, we don’t know what type of variable this memory is.

Contract: When we are usingvoid*When we are doing this, it is best to figure out the information pointed to by the current pointer, which can be done by clear variable names.

This is the end of this article about in-depth understanding of void* in C language. For more related void* content in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!