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 9019687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:50:29+00:00 2026-06-16T04:50:29+00:00

please consider the following code : #include stdafx.h #include <stdio.h> #include <assert.h> #include <stdlib.h>

  • 0

please consider the following code :

#include "stdafx.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

struct Person {
    char *name;
    int age;
    int height;
    int weight;
};

struct Person *Person_create(char *name, int age, int height, int weight)
{
    struct Person *who = (struct Person*) malloc(sizeof(struct Person));
    assert(who != NULL);

    who->name = strdup(name);
    who->age = age;
    who->height = height;
    who->weight = weight;

    return who;
}

the curious line is

struct Person *who = (struct Person*) malloc(sizeof(struct Person));

I searched internet a bit for malloc() usages. about half of them are written with casting, others are not. on vs2010, without (struct Person*) cast an error emerges:

1>c:\users\juhyunlove\documents\visual studio 2010\projects\learnc\struct\struct\struct.cpp(19): error C2440: 'initializing' : cannot convert from 'void *' to 'Person *'
1>          Conversion from 'void*' to pointer to non-'void' requires an explicit cast

So what is a proper way to create a pointer and assign memory to it?

  • 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-16T04:50:30+00:00Added an answer on June 16, 2026 at 4:50 am

    Because you are using a C++ compiler.

    Casting malloc (assuming the type is not void *) is required in C++. In C, it is not required and it is even recommended to not cast malloc.

    In C there is an implicit conversion from void * to all object pointer types during assignment.

    void *p = NULL;
    int *q = p;  // valid in C, invalid in C++
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following code: #include <stdio.h> int main() { static int counter=5; printf
Please consider the following Code: #include <stdio.h> int main() { int x; printf (\nEnter
Please consider the following code: #include <stdio.h> #include <stdlib.h> #define NUM_ARRAYS 4 #define NUM_ELEMENTS
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following piece of code #include <stdio.h> #define ROW_SIZE 2 #define COL_SIZE
Please consider the following code. typedef struct{ int field_1; int field_2; int field_3; int
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Please consider the following code: NSString *string = @ä; const char *str1 = [string
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<

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.