General syntax of defining a malloc() function is void *malloc(size_t size). malloc() returns a void pointer. The calloc() function void *calloc(size_t n_elements, size_t size) also needs to be typecast while declaring. It sets all the memory blocks to zero by default unlike malloc().
For storing it in the pointer of required data type (int, float, char etc.) we have to typecast it. Until we typecast a memory allocation the compiler will not get to know that what type of data we are gonna store to through the pointer. That’s why it’s important to typecast.
Lets take an example
Example
/* C program to understand need of typecasting in DMA */
/* www.codewindow.in */
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
int n, i;
int *p; // Integer pointer is declared
printf("Enter a valid range: ");
scanf("%d", &n);
p=(int*)malloc(n*sizeof(int)); // An int array of n elements has been assigned to the pointer
if(p == NULL) { // Checking if memory allocation is successful or not
printf("\nMemory allocation unsuccessful");
exit(0);
}
else { // Else memory allocation is successful
printf("\nMemory allocation successful");
printf("\nEnter the elements of array: ");
for(i=0; i<n; ++i)
scanf("%d", p+i); // Array elements are taken as input
printf("\nElements of the array are: ");
for(i=0; i<n; i++)
printf("%d ", *(p+i)); // Print element of the array
}
return 0;
}
Input
Enter a valid range: 10
Enter the elements of array: 1 2 3 4 5 6 7 8 9 10
Output
Memory allocation successful
Elements of the array are: 1 2 3 4 5 6 7 8 9 10
Explanation
In this above program, “p” is an integer array pointer. n is the number of elements for which memory has to be allocated. Before using in malloc() function “p” has been declared as a integer pointer, but again “p” is typecast inside malloc() as an integer type pointer.
As malloc() and calloc() both return void pointer if not typecast. But p is an integer pointer. Here the typecasting has been done to resolve this mismatch. The similar logic can be given for explaining the need of typecasting in calloc().
Follow Us
You Missed
- UKG Hiring for Software Engineer
- 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
- 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
- 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
- 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
- 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
- Wipro is Hiring Test Engineers to Elevate Quality Assurance – codewindow.in
- Deloitte Is Hiring: Analysts and Senior Analysts Wanted to Drive Innovation – codewindow.in
- Exciting Software Development Opportunity At Oracle – codewindow.in
- PayPal Hiring Data Analyst 1 – codewindow.in
- SIEMENS HEALTHINEERS Hiring Data Scientist ( ML & AI) – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Unstop Hiring Challenges Internships and Hackathons – codewindow.in
- Programming in Python – codewindow.in
- Programming in Python – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Data Structure – comprehensive guide – codewindow.in
Also Checkout
- Adobe (1)
- Advanced Coading (1)
- Advanced course (1)
- Ajax (17)
- Algorithm (6)
- Angular JS (1)
- Aptitude (10)
- Aptitude tricks (3)
- Automata Fixing (1)
- big data (1)
- Books (1)
- Bridge2i (1)
- C programming (19)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- Cloud Computing (1)
- code nation (2)
- Coding Questions (240)
- Cognizant Placement (11)
- commvault Systems (1)
- Computer Network (1)
- CSS (44)
- Data Science (2)
- Data Structure (1)
- Data Structure and Algorithm (126)
- DBMS (1)
- deloitte (2)
- Epam Full Question Paper (6)
- Extempore (1)
- Exxon Mobil interview questions (1)
- filpkart (1)
- Genpact (1)
- Grab (1)
- Group Discussion (1)
- Guidance for Accenture (3)
- Hackathon 2024 (1)
- Hexaware (1)
- HR Questions (11)
- HTML5 (44)
- IBM Questions (5)
- Infosys (10)
- Internship (1)
- Interview Experience (19)
- Interview Questions (29)
- ITC Infotech (1)
- itron (1)
- JavaScript (2)
- JECA (1)
- Job Info (66)
- JQuery (15)
- Language Confusion (1)
- language confussion (1)
- Linkedin (1)
- Machine Learning (2)
- Media.net (1)
- Miscellaneous (61)
- Mock Test Series (1)
- MongoDB (34)
- nagarro (5)
- navi (1)
- NodeJS (24)
- Operating System (18)
- Optum (1)
- PayU (2)
- PHP and MYSQL (31)
- Programming in C (61)
- Programming in C++ (33)
- Programming in JAVA (154)
- Programming in Python (133)
- Pseudo Code (1)
- pseudocode (5)
- Python (60)
- Quiz (9)
- ReactJS (1)
- Recruiting Companies (34)
- Revature (3)
- salesforce (1)
- Samsung (1)
- Schlumberger (1)
- Seimens (2)
- Software Engineering (2)
- Study Material (4)
- 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)
- Tiger Analytics (1)
- Uncategorized (66)
- UnDosTres (1)
- Unstop (1)
- Verbal Ability (8)
- Verbal Lesson (1)
- Web Development (143)
- wipro (1)
- Wipro Coding Questions (5)
- Wipro NLTH (30)
- WIpro NLTH Coding Solve (19)