Skip to main content

Command Palette

Search for a command to run...

Void pointer

Published
2 min readView as Markdown
Void pointer
X

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.

"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."

In C, void* is a generic pointer type that can be used to store the address of any type of object. It is a special pointer type that does not have an associated type. The void* pointer can be used to provide flexibility and enable generic programming in C.

A good example of using void*
Generic Function Parameters: If you have a function that needs to accept arguments of different types, you can use void* as the parameter type. This allows you to pass in a pointer to any object and cast it to the appropriate type within the function.

void printValue(void* data, char type) {
    switch (type) {
        case 'i': {
            int* intValue = (int*)data;
            printf("Integer value: %d\n", *intValue);
            break;
        }
        case 'f': {
            float* floatValue = (float*)data;
            printf("Float value: %f\n", *floatValue);
            break;
        }
        // Handle other types...
    }
}

int main() {
    int x = 10;
    float y = 3.14;
    printValue(&x, 'i');
    printValue(&y, 'f');
    return 0;
}

The following program will produce an undefined behaviour

int x = 10;
void* voidPtr = (void*) &x;
float* floatPtr = (float*) voidPtr;

This code is technically valid, but it is important to note that dereferencing floatPtr to access the value will still result in undefined behavior.

Here's what happens:

  1. int x = 10;: Here, an integer variable x is declared and assigned the value 10.

  2. void* voidPtr = (void*) &x;: The address of x is taken using the & operator, which gives the memory address where the variable x is stored. Then, a cast is performed (void*) to convert this address to a generic void* pointer type. A void* pointer is capable of holding the address of any type of object.

  3. float* floatPtr = (float*) voidPtr;: The void* pointer voidPtr is then cast to a float* pointer. This means that the voidPtr is being interpreted as a pointer to a float. However, this type of cast is potentially unsafe and may lead to undefined behavior.

The important point to note is that the cast from void* to float* is not a guaranteed safe conversion. It relies on the assumption that the original memory location (&x) holds a value that is correctly represented as a float. If the original memory location does not contain a valid float value, accessing it through floatPtr will result in undefined behavior.

If you need to convert an int value to a float, you should use explicit type conversion:

int x = 10;
float floatVal = (float) x;

This will ensure the correct conversion from int to float without any potential issues or undefined behavior.

More from this blog

PERSONAL BLOG

110 posts

Use the search button to search for a specific topic or keyword *all posts are updated on the go*