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

  • Home
  • SEARCH
  • 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 8969313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:34:28+00:00 2026-06-15T17:34:28+00:00

I have a structure as below typedef struct Mystruct{ char *name; int telno; struct

  • 0

I have a structure as below

typedef struct Mystruct{
char *name;
int telno;
struct Mystruct *nextp;
}data;

Now I malloc the structure

data *addnode;
addnode = malloc (sizeof(data));

Now I would add data to the char *name.

addnode->name = malloc (sizeof(MAX));

Question:Why is it required to malloc again?

I was under the assumption that malloc-ing the addnode will even allocate the memory for addnode->name but it is not so.

  • 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-15T17:34:29+00:00Added an answer on June 15, 2026 at 5:34 pm

    Allocating memory for Mystruct provides enough memory for a pointer to name. At this point we have no idea how many characters will be in a name so can’t possibly allocate the memory for it.

    If you want to fully allocate the structure in a single allocation, you could decide on a max size for name and change the structure definition to

    #define MAX_NAME (10) /* change this as required */
    typedef struct Mystruct{
        char name[MAX_NAME];
        int telno;
        struct Mystruct *nextp;
    }data;
    

    Or, if you know the name when you allocate the struct, you could hide the need for two allocations from the caller by providing a constructor function

    struct Mystruct* Mystruct_create(const char* name)
    {
        Mystruct* ms = malloc(sizeof(*ms));
        ms->name = strdup(name);
        return ms;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the STRUCT1 Structure declared as below typedef struct struct1 { short int
Assume that I have a structure as below: struct line { int length; char
My structure definition is, typedef struct { int taxid; int geneid; char goid[20]; char
I have a structure as shown below. typedef struct { attribute_code_t field_id; uint8_t instance_num;
I have structure like this below. Now, I want swap 2 structures. public struct
I have this structure: typedef struct { char *str; ... } nameType; I create
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a mysql database with a table structure like below : Table Name
I have a data structure which is as given below: class File { public
I have seen many programs consisting of structures like the one below typedef struct

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.