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

C में Void Pointer: Generic Pointer Examples के साथ

Void pointer (void *) किसी भी data type का address रख सकता है. Dereference से पहले typecasting क्यों ज़रूरी है और malloc इसे कैसे use करता है — सीखें.

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

C में void pointer क्या है?

Void pointer (void *) एक generic pointer है जो किसी भी data type का address रख सकता है, लेकिन dereference से पहले उसे specific pointer type में typecast करना ज़रूरी है.

malloc void pointer क्यों return करता है?

ताकि एक ही function हर type के काम आए: return हुआ void* caller की ज़रूरत के हिसाब से int*, char*, struct* या किसी भी pointer type में convert हो जाता है.

क्या void pointers पर pointer arithmetic allowed है?

Standard C में नहीं, क्योंकि sizeof(void) undefined है; GCC extension के रूप में element size 1 byte मानकर allow करता है, लेकिन portable code में पहले cast करें.