int num;
ptr = #
Here, ptr is a pointer to void, meaning it points to a memory location but has no specific type.
If we want to assign a value to the variable num through the pointer ptr, or access num using ptr, we must perform proper type casting, because ptr is a void* and has no type information.
*(int *)ptr = 33; // assign 33 to num via ptr. *ptr = 33; is error.
Here, the leftmost * is the indirection (dereference) operator — it accesses the value stored at the address and the (int *) part typecasts the void* pointer into an int* pointer before dereferencing.
Program : 1
void main()
{
void *ptr;
int num;
ptr = #
*(int*)ptr = 23;
printf("Value : %d ",num);
printf("Value : %d",*(int*)ptr);
}
Output:
Value : 23
Value : 23
Program : 2
void main()
{
void *ptr;
float num;
ptr = #
*(float*)ptr = 65.87;
printf("Value : % .2f ",num);
printf("Value : % .2f",*(float*)ptr);
}
Output:
Value : 65.87
Value : 65.87
This way, by casting void* to the correct type pointer, we can both assign and access values stored in different variable types through the same pointer.
Program : 3
//GENERIC FUNCTION USING POINTER.
void printValue(void *,char);
void main()
{
int a = 33;
float b = 69.14;
char h = 'P';
printValue(&a,'i');
printValue(&b,'f');
printValue(&h,'c');
}
void printValue(void *ptr,char type)
{
switch(type)
{
case 'c':
printf("Character Value : %c ", *(char*)ptr);
break;
case 'i':
printf("Integer Value : %d ", *(int*)ptr);
break;
case 'f':
printf("Float Value : %.2f ", *(float*)ptr);
break;
default:
printf("Sorry | Invalid Type.");
}
}
Output:
Integer Value : 33
Float Value : 69.14
Character Value : P
C Language | Allowed? | C++ Language | Allowed? |
---|---|---|---|
T* ? void* (Assigning of any typed pointer to void pointer) | Yes Implicit, no casting needed. | T* ? void* (Assigning of any typed pointer to void pointer) | Yes Implicit, no casting needed. |
void* ? T* (Assigning of void pointer to any typed pointer) | Yes Implicit, no casting needed. | void* ? T* (Assigning of void pointer to any typed pointer) | No Explicit, casting needed. |
Program : 4
// Myfile.c
void main()
{
int num=33;
int *ptr1,*ptr2;
void *ptr3;
ptr1=#
ptr3=ptr1; //T* ? void* (implicit)
ptr2=ptr3; //void* ? T* (implicit)
printf("Value : %d ",num);
printf("Value : %d ",*ptr1);
printf("Value : %d ",*ptr2);
printf("Value : %d ",*(int*)ptr3);
}
Output:
Value : 33
Value : 33
Value : 33
Value : 33
Program: 5
//Myfile.cpp
int main()
{
int num=33;
int *ptr1,*ptr2;
void *ptr3;
ptr1=#
ptr3=ptr1; //T* ? void* (implicit)
//ptr2=ptr3; Error(Not implicit)
ptr2=(int*)ptr3; //Explicit cast required
printf("Value : %d ",num);
printf("Value : %d ",*ptr1);
printf("Value : %d ",*ptr2);
printf("Value : %d ",*(int*)ptr3);
return 0;
}
Output:
Value : 33
Value : 33
Value : 33
Value : 33
Thank you so much for taking the time to read my Computer MCQs Section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.