The Gateway to Computer Science Excellence
0 votes
163 views
Why we need to type cast  void pointer for accessing the data of variable?
in Programming by (21 points) | 163 views
0
A void pointer is a pointer that has no associated data type with it.

A void pointer can hold address of any data type and can be typecasted to any type.

Void pointer can not be dereferenced so we need type casting of void pointer

To accessing the data of variable
0
As you mentioned void pointer can not be dereferenced .

Why can not dereferenced
0
It is a fact of void pointer .if you want more about deferenced of void pointer .you can refer stack overflow

2 Answers

0 votes

consider an integer pointer like,

int *p;

if I consider an integer takes 2 bytes of memory  then pointer p is supposed to dereference 2 bytes only.

but when you say,

void *p;

then pointer p does not know how many bytes of memory to dereference.

So, you must cast the void pointer to tell him that he is supposed to dereference only 2 byte like,

void *p;

*(int *)p;

in order to make p behave like an integer pointer.

note: same thing is applicable for char float etc.

by (61 points)
0
Please provide content for understanding the concept of dereferencing
0 votes

VOID POINTER: A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type.

Void Pointer performs the same task of storing the address of some variable, but the difference come in its data type association. It's a general purpose pointer, it can store the address of character datatype, float datatype, integer datatype, etc. No datatype is associated with it.

and we need to know what is typecasting,

TYPECASTING: A typecasting is basically a conversion from one type to another. There are two types of type conversion:

(1) Implicit Type Conversion: Done by the compiler on its own, without any external trigger from the user.

example:

(2) Explicit Type Conversion: This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type.

 example: Explicit type conversion

                 

#include<stdio.h>

  

int main()

{

    double x = 1.2;

  

    // Explicit conversion from double to int

    int sum = (int)x + 1;

  

    printf("sum = %d", sum);

  

    return 0;

}

output: sum = 2

(src:geeksforgeeks)

Now the question is that why we need to typecast.

Let us take an example,

int a = 5;

int *p = &a;

printf("%d", *p); // it will simply print the data value stored in a and that it pointing by p.

Void Pointer perform the same task of storing the address of some variable, but the difference come in its data type association

int a = 5;

char c = 'x' ;

float f = 5.9;

void* p1 = &a; // no problem

void* p2 = &c; // no problem

void* p3 = &f; // no problem

printf("%d",*p1); // here compiler will confuse about data type and gives error: dereferencing 'void*' pointer invalid use of void expression

printf("%c",*p2); // here compiler will confuse about data type and gives error: dereferencing 'void*' pointer invalid use of void expression

printf("%f",*p3); // here compiler will confuse about data type and gives error: dereferencing 'void*' pointer invalid use of void expression

thats why we need to type cast void pointer.

and writing the above code in the following way gives no error.

printf("%d",*(int*)p1); // no error

printf("%c",*(char*)p2); // no error

printf("%f",*(float*)p3); // no error

 

 

 

by Active (1.5k points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,390 answers
198,589 comments
105,443 users