Why the dereference operator is used with the void keyword while declaring a void pointer?

Debojit Acharjee 455 Reputation points
2023-07-26T15:18:38.5366667+00:00

Why the indirection/dereference operator (*) is used with the "void" keyword while declaring a void pointer? But in case of any other type of pointers the operator can be used with the pointer name.

#include <stdio.h>

int main()
{
    int a = 1;
    int *a2_ptr = &a;
    void* a_ptr = &a;

    printf("Value of 'a' from void pointer is %d and int pointer is %d\n", *(int*)a_ptr, *a2_ptr); // Dereferencing of void pointer using type casting

    return 0;
}

In the above program, the integer pointer "a2_ptr" is declared using the * operator with the pointer name but in case of the void pointer, why the operator is used with the "void: keyword?

Developer technologies | C++
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

Accepted answer
  1. Barry Schwarz 3,746 Reputation points
    2023-07-27T07:08:08.99+00:00

    Because void and void* have different meanings. You use the one that matches what you are trying to define.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2023-07-26T15:43:23.9133333+00:00

    The void* is used to declare an address pointer, where the type that the address points to is unknown.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.