Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9154625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:27:25+00:00 2026-06-17T12:27:25+00:00

Possible Duplicate: Do I cast the result of malloc? What is the Best Practice

  • 0

Possible Duplicate:
Do I cast the result of malloc?
What is the Best Practice for malloc?

Im new with c language and i have a question
as i was explained in class:

The type of the pointer returned by malloc() is void *, and therefore
we need to cast it into the required type

which means i need to do:

char *str;
str = (char *) malloc(14);

but i dont understand, is it a must? lets say malloc returns that void pointer, why do i see many of examples around without doing the casting? an example:

float *arr = malloc (20 * sizeof (float));

Could anyone please explain 🙂 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:27:26+00:00Added an answer on June 17, 2026 at 12:27 pm

    In C, this cast is not required. The conversion is indeed implicit.

    C11 (n1570), § 6.3.2.3 Pointers

    A pointer to void may be converted to or from a pointer to any object type. A pointer to
    any object type may be converted to a pointer to void and back again; the result shall
    compare equal to the original pointer.

    Since it is unecessary, cast malloc return or not is another question (see here).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Do I cast the result of malloc? I just learned how to
Possible Duplicate: Do I cast the result of malloc? I was googling to find
Possible Duplicate: PHP unexpected result of float to int type cast int((0.1+0.7)*10) = 7
Possible Duplicates: Do I cast the result of malloc? Should I explicitly cast malloc()'s
Possible Duplicate: How do you cast a List of objects from one type to
Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation
Possible Duplicate: Scala map containing mix type values I have a situation where in
Possible Duplicate: CAST and IsNumeric I have a database column that is nvarchar, but
Possible Duplicate: Is it ok to cast a STL container with Base type to
Possible Duplicate: pthread Function from a Class I have this code that I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.