Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C Pointers · 03 Jul 2026 · Hindi + English

Void Pointer in C: Generic Pointer with Examples

A void pointer (void *) can hold the address of any data type. Learn why it needs typecasting before dereferencing and how malloc uses it.

What is a void pointer?

A void pointer (void *) is a generic pointer that can store the address of any data type — int, float, char, struct, anything. Its trade-off: you cannot dereference it directly, because the compiler doesn't know what type lives at that address.
int x = 10; float f = 3.5f; char c = 'A';

void *vp;
vp = &x;    /* OK */
vp = &f;    /* OK */
vp = &c;    /* OK - one pointer, any type */

Why direct dereference fails

void *vp = &x;
printf("%d", *vp);          /* ERROR: invalid use of void expression */

To read 4 bytes as int or 8 bytes as double, the compiler must know the type. A void pointer says "address only, type unknown" — so you must typecast before dereferencing:

printf("%d", *(int*)vp);    /* OK: cast to int* first */

Complete example

#include <stdio.h>

int main() {
    int   x = 10;
    float f = 3.5f;
    void *vp;

    vp = &x;
    printf("int value   : %d\n", *(int*)vp);

    vp = &f;
    printf("float value : %.1f\n", *(float*)vp);
    return 0;
}
int value : 10 float value : 3.5

Where void pointers are used in real C

  • malloc / calloc: they return void * precisely so one function can allocate memory for any type: int *p = (int*)malloc(4);
  • Generic functions: qsort() and bsearch() take void * so they can sort arrays of any type.
  • memcpy / memset: raw-memory functions accept void * because bytes have no type.

Rules and limitations

  • No direct dereference: always cast first — *(type*)vp.
  • No pointer arithmetic in standard C: vp + 1 is invalid because the size of "void" is unknown (GCC allows it as an extension, treating it as 1 byte — do not rely on this in exams).
  • Any pointer converts to void* and back without a cast in C — that's why int *p = malloc(4); also works without (int*) in pure C.

Void pointer क्या है?

Void pointer (void *) एक generic pointer है जो किसी भी data type का address store कर सकता है — int, float, char, struct, कुछ भी. Trade-off: इसे direct dereference नहीं कर सकते, क्योंकि compiler को नहीं पता उस address पर कौन-सा type रहता है.
int x = 10; float f = 3.5f; char c = 'A';

void *vp;
vp = &x;    /* OK */
vp = &f;    /* OK */
vp = &c;    /* OK - एक pointer, कोई भी type */

Direct dereference fail क्यों होता है

void *vp = &x;
printf("%d", *vp);          /* ERROR: invalid use of void expression */

4 bytes को int या 8 bytes को double की तरह पढ़ने के लिए compiler को type पता होना चाहिए. Void pointer कहता है "सिर्फ address, type unknown" — इसलिए dereference से पहले typecast ज़रूरी है:

printf("%d", *(int*)vp);    /* OK: पहले int* में cast */

पूरा Example

#include <stdio.h>

int main() {
    int   x = 10;
    float f = 3.5f;
    void *vp;

    vp = &x;
    printf("int value   : %d\n", *(int*)vp);

    vp = &f;
    printf("float value : %.1f\n", *(float*)vp);
    return 0;
}
int value : 10 float value : 3.5

Real C में void pointers कहां use होते हैं

  • malloc / calloc: ये void * return करते हैं ताकि एक ही function किसी भी type के लिए memory allocate कर सके: int *p = (int*)malloc(4);
  • Generic functions: qsort() और bsearch() void * लेते हैं ताकि किसी भी type का array sort कर सकें.
  • memcpy / memset: raw-memory functions void * accept करते हैं क्योंकि bytes का कोई type नहीं होता.

Rules और Limitations

  • Direct dereference नहीं: हमेशा पहले cast करें — *(type*)vp.
  • Standard C में pointer arithmetic नहीं: vp + 1 invalid है क्योंकि "void" का size unknown है (GCC extension के तौर पर 1 byte मानकर allow करता है — exams में इस पर भरोसा न करें).
  • C में कोई भी pointer void* में और वापस बिना cast के convert होता है — इसीलिए pure C में int *p = malloc(4); बिना (int*) के भी चलता है.

Frequently Asked Questions

What is a void pointer in C?

A void pointer (void *) is a generic pointer that can hold the address of any data type, but it must be typecast to a specific pointer type before dereferencing.

Why does malloc return a void pointer?

So one function can serve every type: the returned void* converts to int*, char*, struct* or any other pointer type the caller needs.

Is pointer arithmetic allowed on void pointers?

Not in standard C, because sizeof(void) is undefined; GCC allows it as an extension treating element size as 1 byte, but portable code should cast first.