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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:45:34+00:00 2026-05-19T00:45:34+00:00

Hello i’m not sure if I’m understand the following piece of code. I would

  • 0

Hello i’m not sure if I’m understand the following piece of code. I would glad if someone could read my explanations and correct me if I’m wrong.

So first of all I’m declaring a struct with three arrays of char and an integer.

struct Employee 
{
    char last[16];
    char first[11];
    char title[16];
    int salary; 
};

After that I declare a function which takes three pointers to char and an integer value. This function uses malloc() and sizeof() to create a struct on the heap. Now this creations of the object on the heap is not really clear to me. When I use struct Employee* p = malloc(sizeof(struct Employee)), what happens there exactly?
What happens when I use the function struct Employee* createEmployee (char* last, char* first, char* title, int salary) several times with different input. I know that I will get back a pointer p but isn’t that the same pointer to the same struct on the heap. So do I rewrite the information on the heap, when I use the function several times? Or does it always create a new object in a different memory space?

struct Employee* createEmployee(char*, char*, char*, int);

struct Employee* createEmployee(char* last, char* first, char* title, int salary)  
{
    struct Employee* p = malloc(sizeof(struct Employee));
    if (p != NULL) 
    {
        strcpy(p->last, last);
        strcpy(p->first, first);
        strcpy(p->title, title);
        p->salary = salary;
    }
    return p; 
}

I would be glad if someone could explain it to me. Thank you very much.

  • 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-05-19T00:45:35+00:00Added an answer on May 19, 2026 at 12:45 am

    The malloc function allocates some new bytes on the heap and returns the pointer.

    So the createEmployee function allocates new memory every time it’s called, then fills it with some data (in an unsafe way – consider using strncpy instead) and returns the pointer to that memory. It will return a different pointer every time it’s called.

    Each instance you create with this function will exist as long as you don’t call free on its pointer.

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

Sidebar

Related Questions

Hello I use the following code in order to retrieve an events infoormation from
Hello and thanks for the help. I am using the following code to set
Hello I have the following script in PHP: $correct = array(a, a, a, a);
Hello i have simply function to read from file while(fscanf(fp, %255[a-zA-Z],test) == 1) {
Hello i have a special timezone like Europe/Berlin and i would like to add
Hello i need to gater the data from db(MSSQL) in following way: <div id=slider>
Hello again; i need to show X,Y,Risk in ListBoxes. But i can not do
Hello I am using this code to get the data I need from 5
Hello I want to collect output of following command into some variable.How can I
Hello i have a weird doubt.. MY code is def prints(): print I WILL

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.