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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:29:14+00:00 2026-05-31T13:29:14+00:00

i asked a previous question on this website and it got answered using pseudo

  • 0

i asked a previous question on this website and it got answered using pseudo code, yet I still cant figure out how to properly solve this problem

basically i pass an array of characters, and a number that the user selects that correlates to how many new characters to add to the array. I want to create a new array with size = the old array + the new number of characters to add, prompt user for the new chars, and then add it to the new array (with the old chars in it reallocated). I dont know how to do this! and am frustrated.

char * add(char * array, int num)
{
 /* malloc new_size bytes and assign to new_array
memcpy old_size bytes from old_array into the new_array
add additions into new_array starting from (new_array+old_size)
free the old_araray
return new_array; 
*/

}  
  • 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-31T13:29:15+00:00Added an answer on May 31, 2026 at 1:29 pm

    You tagged the question “realloc” so presumably you’re aware of the realloc() function. It’d be simpler, and possibly more efficient, to use that instead of malloc(), memcpy(), and free()

    What I don’t see here, though, is how the function knows the size of the “old” array. Is it a null-terminated string? If not, you’ll need to pass another integer that says how big the existing array is.

    Assuming it’s a null-terminated string, you could do something like this:

    char *add(char *string, int num) {
      // Note, these represent the length *without* the null terminator...
      int old_length = strlen(string);
      int new_length = old_length + num;
    
      // ...so we add 1 here to make room for the null.
      string = realloc(string, new_length + 1);  // Error checking omitted
    
      for (int n = old_length; n < new_length; n += 1) {
        // Prompt for the new characters; here I'll just assume they're all 'X'.
        char new_char = 'X';
    
        string[n] = new_char;
      }
    
      string[new_length] = '\0';
    
      return string;
    }
    

    If it’s not a null-terminated string, you’d pass in old_length as an argument rather than determining it with strlen(), don’t add 1 in the realloc() call, and don’t set string[new_length] to a null at the end. The rest stays the same.

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

Sidebar

Related Questions

this question is an extension to a previous question i asked (and was answered).
This stems from a previous question I asked - about a write conflict with
This is a continuation question from a previous question I have asked I now
This is a follow up question to a previous question I asked about calculating
This is a related to a previous question I have asked here, see the
In a previous question, I asked about various ORM libraries. It turns out Kohana
@Spence asked this Previous Question . So, how's that work in Java? Generic types
This question is a folo to a previous question I asked about how to
This question is related to a previous question I asked, but it's a different.
This question is an updated version of a previous question I have asked on

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.