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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:28:06+00:00 2026-06-02T23:28:06+00:00

I am supposed to follow the following criteria: Implement function answer4 (pointer parameter and

  • 0

I am supposed to follow the following criteria:

Implement function answer4 (pointer parameter and n):

  1. Prepare an array of student_record using malloc() of n items.

  2. Duplicate the student record from the parameter to the array n
    times.

  3. Return the array.

And I came with the code below, but it’s obviously not correct. What’s the correct way to implement this?

student_record *answer4(student_record* p, unsigned int n)
{
    int i;
    student_record* q = malloc(sizeof(student_record)*n);
    for(i = 0; i < n ; i++){
        q[i] = p[i];
    }
    free(q);
    return q;
};
  • 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-02T23:28:07+00:00Added an answer on June 2, 2026 at 11:28 pm
    p = malloc(sizeof(student_record)*n);
    

    This is problematic: you’re overwriting the p input argument, so you can’t reference the data you were handed after that line.

    Which means that your inner loop reads initialized data.

    This:

    return a; 
    

    is problematic too – it would return a pointer to a local variable, and that’s not good – that pointer becomes invalid as soon as the function returns.

    What you need is something like:

    student_record* ret = malloc(...);
    
    for (int i=...) {
     // copy p[i] to ret[i]
    }
    
    return ret;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On the following page I have a popup that is supposed to show beside
I'm using C# to manipulate an Excel worksheet. The following two pieces of code
Following this SO post , I am trying to render indented comments using the
I have several UIView subclasses (buttons, labels, etc.) that follow the following setup pattern.
Supposed I am pulling the synonyms of help by the function of synonyms() from
I am using JQuery with the Star Rating Plugin from the following site http://www.fyneworks.com/jquery/star-rating/#
this is a follow-up question of mine. Suppose now I have a URL :
Suppose I setup test2.js as follows module.exports.doPrint = doPrint; var dummy = initial; function
This is a follow up to my previous question . Consider that I write
I'm following the RoR 3 Rails Tutorials, but to upgrade to a newer version

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.