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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:02:52+00:00 2026-05-13T14:02:52+00:00

I am writing my own string copy function. The following works: char *src, *dest;

  • 0

I am writing my own string copy function. The following works:

char *src, *dest;
src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
dest = (char *) malloc(strlen(src) + 1);
mystringcpy(src, dest);

void mystringcopy(char *src, char *dest) {
   for(; (*dest = *src) != '\0'; ++src, +dest);
}

But this doesn’t work:

char *src, *dest;
src = (char *) malloc(BUFFSIZE);
//Do something to fill the src
mystringcpy(src, strlen(src), dest);

void mystringcopy(char *src, size_t length, char *dest) {
   dest = (char *)malloc(length + 1);
   for(; (*dest = *src) != '\0'; ++src, +dest);
}

and I can’t understand why… Is allocating memory inside a called function a mistake?

  • 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-13T14:02:53+00:00Added an answer on May 13, 2026 at 2:02 pm

    You haven’t really said what “works” means, but I’m assuming you’re confused why dest isn’t being changed to the new memory back in the calling function.

    The reason is that in your mystringcopy function, the parameter dest is a copy of the pointer dest in the calling function.

    You then assign that copy to a new buffer, do the copy, and then the copy goes away. The original is unchanged. You need to pass dest as a pointer (to a pointer).

    Also, I assume you wrote what you did from memory since it shouldn’t compile as is (bad dereference in the calling function). Here’s the fixed code:

    char *src, *dest;
    src = (char *)malloc(BUFFSIZE); // no dereference on src, it's a pointer
    
    //Do something to fill the src
    mystringcpy(src, strlen(src), &dest); // pass the address of dest
    
    // take a pointer to a char*
    void mystringcopy(char *src, size_t length, char **dest) {
        // now you should dereference dest, to assign to
        // the char* that was passed in
        *dest = (char *)malloc(length + 1);
    
        // for simplicity, make an auxiliary dest
        char* destAux = *dest;
    
        // and now the code is the same
        for(; (*destAux = *src) != '\0'; ++src, ++destAux);
    }
    

    Another method is to return the dest pointer:

    char *src, *dest;
    src = (char *)malloc(BUFFSIZE);
    
    //Do something to fill the src
    dest = mystringcpy(src, strlen(src)); // assign dest
    
    char* mystringcopy(char *src, size_t length) {
        char* dest = (char *)malloc(length + 1);
    
        // for simplicity, make an auxiliary dest
        char* destAux = dest;
    
        for(; (*destAux = *src) != '\0'; ++src, ++destAux);
    
        return dest; // give it back
    }
    

    Keep in mind if length is smaller than the source buffer’s real length that you’ll overrun your destination buffer. See the comments for a solution, though this is left up to you.

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

Sidebar

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.