what is void pointer?
I am unable to understand, please help me

Recommended Answers

All 5 Replies

there are basically 5 data types in C.
Int, Char, Float , Double and Void.

The type Void either explicitly declares a function as returning no values or creates generic pointers.

when we declare

int i;
int *p;
p=&i;

when we declare p as an integer pointer means it will store the address of integer type data.
As I told Void pointer is a generic pointer which means it can store the address of other types as required.
for eg.

#include<stdio.h>
int main()
{
    int i=5;
    char c='A';
    void* p;
    p=&i;
    printf("%d  ",*(int*)p);
    p=&c;
    printf("%c  ",*(char*)p);
    return 0;
}

here as you see first address of int is stored in void * p.
to access the value at that address you have to typecast with the type which is stored in that void ptr.
here to access value at p we have to use
*(int*)p;

cse.avinash
Where to use this void pointer in a part of a program.This is big doubt having in my mind. please help me.:?:

Think of a void pointer as a dimensionless pointer. It has to be cast to a data type with a dimension before it can be referenced...Like below.

int x = 5;
void *vptr = (void*)&x;
int *iptr = (int*)vptr;
fprintf(stdout, "*iptr->%d\n", *iptr);

We can make a generalized functions with the help of void pointer like this:-

#include <stdio.h>
#include <stdlib.h>
void abc(void *a, int b) {
  char *format[] = {"%d ", "%c "};
  printf(format[b-1], a);
}
int main()
{
    abc(1,1);
   abc('A',2);
        return 0;
}

check this link for more information..:)

Thanks avinash for the link, My confusions are clear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.