Void pointer is a generic pointer, it has void as its dataype. It can hold the address of any datatype. While dereferencing through void pointer typecasting is must. In c malloc() and calloc() functions return void pointer. The size of the void pointer in C is the same as the size of the pointer of character type.
Why we use void pointer?
Till now, we have studied that the address assigned to a pointer should be of the same type as specified in the pointer declaration.
For example, if we declare the int pointer, then this int pointer cannot point to the float variable or some other type of variable i.e. it can point to only int type variable. To overcome this problem, we use a pointer to void. Let’s see an example.
#include<stdio.h>
int main() {
int x=10;
char ch="A";
void *gp; //it is a generic pointer
gp=&x; //address of int variable x is stored in gp
printf("%d",*(int*)gp); //explicit typecasting it convert gp to integer pointer
gp=&ch; // address of char variable ch is stored in gp
printf("%c",*(char*)gp);
return 0;
}
Now what is *(int*)gp in above program? Let’s break it step by step:
1. *(int*)gp
2. *(int pointer gp)
3. value at integer pointer gp
4. value at &x which is 10
Points to remember
1. The void pointer in C cannot be dereferenced directly. Let’s see the below example.
#include <stdio.h>
int main() {
int x=80;
void *p;
p=&x;
printf("Value which is pointed by ptr pointer : %d",*p);
return 0;
}
In the above code, *p is a void pointer which is pointing to the integer variable ‘x’. As we already know that the void pointer cannot be dereferenced, so the above code will give the compile-time error because we are printing the value of the variable pointed by the pointer ‘p’ directly.
So, how to overcome the problem?
#include <stdio.h>
int main() {
int x=80;
void *p;
p=&x;
printf("Value which is pointed by ptr pointer : %d",*(int*)p);
return 0;
}
In the above code, we typecast the void pointer to the integer pointer by using the statement (int*)p. Then, we print the value of the variable which is pointed by the void pointer ‘ptr’ by using the statement *(int)*ptr
Output: Value which is pointed by ptr pointer : 80
2. We cannot apply the arithmetic operations on void pointers in C directly. We need to apply the proper typecasting so that we can perform the arithmetic operations on the void pointers.
3. It is always recommended to avoid the using of void pointers unless it is urgent as they effectively allow you to avoid type checking.
You Missed
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in
Also Checkout
- Adobe (1)
- Advanced Coading (1)
- Advanced course (1)
- Ajax (17)
- Algorithm (6)
- Amagi (1)
- Amazon Interview Questions (1)
- Angular JS (23)
- Aptitude (10)
- Aptitude tricks (3)
- Automata Fixing (1)
- Basic Coding (1)
- big data (61)
- Books (1)
- Bridge2i (1)
- C programming (20)
- Campgemini Interview Questions (1)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- Celebal Tech (1)
- Cloud Computing (28)
- code nation (2)
- Coding Questions (240)
- Cognizant Placement (11)
- commvault Systems (1)
- Computer Network (24)
- CSS (44)
- CTS (1)
- Data Science (44)
- Data Structure (1)
- Data Structure and Algorithm (126)
- DBMS (29)
- De Show Interview Questions (1)
- deloitte (2)
- Deutsche Bank Interview questions (1)
- Enhance Communication (1)
- Epam Full Question Paper (6)
- Extempore (1)
- Exxon Mobil interview questions (1)
- filpkart (1)
- Fractal Analytics Interview Questions (1)
- Genpact (1)
- Grab (1)
- GreyB Interview Questions (1)
- Group Discussion (1)
- Guidance for Accenture (3)
- Gupshup (1)
- Hackathon 2024 (1)
- HCL Interview Questions (1)
- Hexaware (1)
- HFCL (1)
- HR Questions (11)
- HTML5 (44)
- IBM Interview questions (1)
- IBM Questions (5)
- Incture Interview Questions (1)
- Infineon Technologies Interview Questions (1)
- Infosys (11)
- Infosys Interview Questions (1)
- Internship (1)
- Interview Experience (19)
- Interview Questions (63)
- ITC Infotech (1)
- itron (1)
- JavaScript (67)
- JECA (1)
- Job Info (93)
- JQuery (15)
- Kantar Interview Questions (1)
- Language Confusion (1)
- language confussion (1)
- Larsen & Turbo (1)
- Latenview AnalyticsInterview questions (1)
- Lexmark International Interview Questions (1)
- Linkedin (1)
- Machine Learning (23)
- Media.net (1)
- Mindtree Interview Questions (1)
- Miscellaneous (61)
- Mock Test Series (2)
- MongoDB (34)
- Morgan Stanly Interview Questions (1)
- nagarro (5)
- navi (1)
- NodeJS (24)
- NTT Data Interview Questions (1)
- NVDIA (1)
- NVDIA interview questions (1)
- Operating System (19)
- Optum (1)
- PayU (2)
- Persistent INterview Questions (1)
- PHP and MYSQL (31)
- Previous Coding Questions (1)
- Programming in C (61)
- Programming in C++ (33)
- Programming in JAVA (154)
- Programming in Python (133)
- Pseudo Code (2)
- pseudocode (5)
- PWC Interview Questions (1)
- Python (61)
- Quiz (9)
- Razorpay (1)
- ReactJS (26)
- Recruiting Companies (34)
- Revature (3)
- salesforce (1)
- Samsung (1)
- Schlumberger (1)
- Seimens (2)
- Slice (1)
- Smart Cube (1)
- Software Engineering (35)
- Study Material (4)
- Tally Solutions (1)
- tata cliq (1)
- TCS (1)
- TCS NQT (69)
- TCS NQT Coding Questions (13)
- Tech Mahindra Coding Questions (4)
- Tech Mahindra Questions (8)
- Technical Preparation (1)
- Teg Analytics (1)
- Tejas Network Interview Questions (1)
- Texas Instrument Interview Questions (1)
- Tiger Analytics (1)
- Uncategorized (66)
- UnDosTres (1)
- Unstop (1)
- Verbal Ability (8)
- Verbal Lesson (1)
- Web Development (208)
- wipro (1)
- Wipro Coding Questions (5)
- Wipro interview Questions (1)
- Wipro NLTH (30)
- WIpro NLTH Coding Solve (19)
- Zenser (1)
- Zoho Interview Questions (1)