What is a void pointer in C programming language?

Definition:
      A pointer that can hold the address of any data type.It can be pointed to any data type by typecasting.It is declared with the void keyword placed in the data type.

Example: void *ptr;


int main()
{
   int a=3;
   char ch='c';
   void *ptr=&a;//pointing to an int
   ptr = ch;//pointing to an char
   return 0;
}




Important Points:

1. void pointer cannot be dereferenced.If you try to do it you will get a compile time error.For example:

int main()
{
    int a;
    void *ptr= &a;
    printf("value:%d\n",*a);
    return 0;
}


2.You cannot perform pointer arithmetic operation on void pointer .For example

int main()
{
   int a;
   void *ptr = &a;
   ptr++;
   return 0;
}

Comments

Popular posts from this blog

Appending and Prepending in Yocto - +=, =+ vs .=, =.

ARM interview questions

Linux driver example for Per CPU Variable