Void pointer in c

Void pointer in c

Its meaning is the pointer is pointing to nothing. It is called a null pointer. Below is the snap shot of the code from stdlib. In below example, we have initialized the pointer with NULL and during accessing the int value from the location we are checking if memory is allocated properly.

If malloc fails it returns NULL. If we try to access value with invalid memory the program will crash or run time error for heap corruption.

Use as a marker — For example, to indicate a pointer that has not been allocated memory or to check end of a linked list or to check if a tree has any child note or leaf etc.

So, we can check the pointer by comparing with NULL, if a valid memory is allocated. Also, check to free a valid memory etc. Generally, the dangling pointers occurs in a program with following steps. Now, this pointer is called dangling pointer. If we process this pointer further in the program then the program will crash as it will not find valid memory. So, It is always a good practice to initialized the pointer with NULL to avoid it to be dangling pointer.

Dangling pointer example:. After freeing the allocated memory, again it will be assigned to NULL to avoid dangling. After freeing memory the iPter is dangling pointer as store the address but that is invalid now.

void pointer in c

Hence, assign it to NULL to avoid dangling pointer. Note that we can store any data type in void pointer, but, when we want to retrieve data from void pointer then we need to type cast to the specific data type.

In below program, we have created a void pointer and assign the address of int variable. Value is retrieved back by typecasting void pointer to int pointer and De-referencing it. Another example of void pointer Example: In below C program, malloc returns a void pointer on memory allocation. Since, we have allocated for integer data type, so, we need to type cast to int pointer type. NULL pointer and void pointer are totally different concepts and no need to compare them. NULL pointer is a valuewhereas void pointer is a type.

Null pointer is a special reserved value of a pointer and any type of pointer can have reserved value. What is size of void pointer? Example: In below example, we have initialized the pointer with NULL and during accessing the int value from the location we are checking if memory is allocated properly.

When done with the memory free or delete the memory location. NULL then it will contain some address which is invalid location now. Dangling pointer example: After freeing the allocated memory, again it will be assigned to NULL to avoid dangling.Pointer is a variable pointing at a memory location of specific type. Type defines many important properties related to the pointer. Such as valid memory addresses it can point, pointer arithmetic, etc.

As per C programming semantics, you must specify pointer type during its declaration. Also, it is illegal to point pointer of one type to object of another type.

For example, int pointer cannot point to a float variable. Type specific pointers are beneficial in different ways. However, in programming there happens situation when you need to go typeless. For example, suppose you are required to design a function that can accept any type value as argument, process data and return results.

For such situation, you need a pointer that must work with all types. A void pointer is a special pointer that can point to object of any type. A void pointer is typeless pointer also known as generic pointer. Note: Writing programs without being constrained by data type is known as generic programming.

A generic function is a special function that focuses on logic without confining to data type. For example, logic to insert values in array is common for all types and hence can be transformed to generic function. Dereferencing is the process of retrieving data from memory location pointed by a pointer. It converts block of raw memory bytes to a meaningful data data is meaningful if type is associated. While dereferencing a void pointer, the C compiler does not have any clue about type of value pointed by the void pointer.

Hence, dereferencing a void pointer is illegal in C. But, a pointer will become useless if you cannot dereference it back. To dereference a void pointer you must typecast it to a valid pointer type. However, some compiler supports void pointer arithmetic by assuming it as a char pointer.

To perform pointer arithmetic on void pointer you must first typecast to other type. Write a C function to accept an array and print its elements. The function must accept array of different types.At the very high level, we can think of NULL as a null pointer which is used in C for various purposes.

By doing so, we can perform error handling in pointer related code e. It should be noted that NULL pointer is different from an uninitialized and dangling pointer. In a specific program context, all uninitialized or dangling or NULL pointers are invalid but NULL is a specific invalid pointer which is mentioned in C standard and has specific purposes.

By specifically mentioning NULL pointer, C standard gives mechanism using which a C programmer can use and check whether a given pointer is legitimate or not. Let us see what C standards say about null pointer. From C11 standard clause 6. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

This is also called the C11 standard. But C standard is saying that 0 is also a null pointer constant. It means that the following is also perfectly legal as per standard. This statement is also from C11 clause 7.

It means that internal representation of the null pointer could be non-zero bit pattern to convey NULL pointer. Having said so, typically NULL is represented as all bits set to 0 only. To know this on a specific platform, one can use the following. Well, usage of sizeof NULL is allowed but the exact size would depend on platform. If the pointer size of a platform is 4 bytes, the output of the above program would be 4. But if pointer size on a platform is 8 bytes, the output of the above program would be 8.

What about dereferencing of NULL? Again it depends on a lot of factors. But the idea of mentioning the above snippet is that we should always check for NULL before accessing it. As per C11 standard clause 6. Even C11 clause 6. On gcc, the above would output 1. Here C11 has mentioned guidelines. From clause 6. Inspite of mentioning machine dependent stuff as above, we as C programmers should always strive to make our code as portable as possible.

So we can conclude on NULL as follows:. Always initialize pointer variables as NULL. Always perform a NULL check before accessing any pointer.Nov 20, at am UTC. I couldn't think of any and I thought I'd explain why. In C, the use of void pointers can be extremely useful. It can be used as a universally generic pointer that can point to any type.

The limitation to this is that you must know the type that the void pointer was casted from in order to obtain data from it which usually isn't that bad. Here's example using a simple list struct. This is a memory leak! Be sure to free when in practice! Make sure to delete the variables when in practice! Prepend ; tmpList.

Last edited on Nov 20, at am UTC. Even that isn't a really good use of void pointers. I mentioned it, I just didn't demonstrate it. Wasn't sure how to go about it properly to be honest but it seems pretty simple. Nov 20, at pm UTC.

void pointer in c

Things like e. These things often operate on "raw" memory, and the type information is useless there. Templates would only bloat the code. Last edited on Nov 20, at pm UTC.

Table of Content

Nov 21, at am UTC. Yeah, after a bit more thought, I don't like the original post. I speak of it as if void pointers and templates are interchangeable, which in some cases they are but not always. Although there is some useful information, the impression it gives is rather off. I won't take down but, just sayingWhat if we can have a pointer that can point to any type of variable? This can be done easily using void pointer. It can contain the address of variable of any data type.

We can declare a void pointer in C using void keyword as shown below. Dereferencing means accessing the value at the address stored in pointer variable. We have to type caste the pointer variable to dereference it because the void pointer is not associated with any data type. The compiler is unable to find the type of variable pointed by the void pointer. This can be done in following way. This is all about void pointer in C.

If you find any mistake or information missing in above tutorial then please mention in the comments. Your email address will not be published. When we declare a pointer we specify its type which will be same as the type of the variable whose address the pointer will contain.

For example if we will declare an integer pointer then it can contain the address of an integer variable only. Take below example. Leave a Reply Cancel reply Your email address will not be published.It would be incorrect, if we assign an address of a float variable to a pointer of type pointer to int.

But void pointer is an exception to this rule. A void pointer can point to a variable of any data type.

void pointer in c

Here is the syntax of void pointer. Here vp is a void pointer, so you can assign the address of any type of variable to it. A void pointer can point to a variable of any data type and void pointer can be assigned to a pointer of any type.

For example:. Before you dereference a void pointer it must be typecasted to appropriate pointer type. Let me show you what I mean. For example: In the above snippet void pointer vp is pointing to the address of integer variable a. Note: typecasting changes type of vp temporarily until the evaluation of the expression, everywhere else in the program vp is still a void pointer.

Another important point I want to mention is about pointer arithmetic with void pointer. Before you apply pointer arithmetic in void pointers make sure to provide a proper typecast first otherwise you may get unexcepted results. Can i assign the value of a void pointer at an another void pointer?

I have searched a lot about dereferencing concept but at last, I got ur article in which my all doubts are clear. Thank you so much to share this useful information.

Notify me of new posts by email. Like this: Like Loading Sorry, your blog cannot share posts by email. We use cookies to ensure that we give you the best experience on our website.

void pointer in c

If you continue to use this site we will assume that you are happy with it. Ok Privacy policy.Before going further it will be good if you refresh about pointers by reading — Introduction to pointers in C.

It is invalid and will result in a compilation error. When a pointer variable is declared using keyword void — it becomes a general purpose pointer variable. Address of any variable of any data type char, int, float etc. We have seen about dereferencing a pointer variable in our article — Introduction to pointers in C. But in the case of a void pointer we need to typecast the pointer variable to dereference it.

This is because a void pointer has no data type associated with it.

What is the size of void pointer in C/C++?

There is no way the compiler can know or guess? So to take the data pointed to by a void pointer we typecast it with the correct type of the data holded inside the void pointers location.

Pointer types, pointer arithmetic, void pointers

A void pointer can be really 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.

A code snippet is given below. Another important point you should keep in mind about void pointers is that — pointer arithmetic can not be performed in a void pointer. Nice explanation. Author jojo. Rogers Guedes 6 years ago. Thank you so much guy! Jitendra 7 years ago. Pointer arithmetic: this example does compile in gcc 4. Maruf 8 years ago. Submit Type above and press Enter to search. Press Esc to cancel.

replies on “Void pointer in c”

Leave a Reply

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