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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:36:23+00:00 2026-06-12T23:36:23+00:00

I have this custom string copy function and I’ve noticed that the *dest argument

  • 0

I have this custom string copy function and I’ve noticed that the *dest argument changes upon dereferencing it and modifying its contents:

char *copyArray(char *dest, char *src)
{
    char *a = dest;

    while (*dest++ = *src++)
        ;   
    char *b = dest;

    if ( (a-b) != 0)  
        printf("Dest pointer has changed\n");

    return dest;                                                                        
}

And if you consider the following code:

int main()
{
    char name [] = "Hello, there!";

    char *new = copyArray(name, "bye");

    printf("New is '%s', name is '%s'\n", new, name); 

    return 0;
}

I get the following output:

Dest pointer has changed
New is 'o, there!', name is 'bye'

Why does this happen?

My intent was for *new to point to the same location name[] points to, but obviously as the address changed, it points to a different place.

I feel that what’s happened is that once the copying ended, C moved the original contents of the destination string minus the number of characters changed (4 for “bye”) to a new address and assigned that to *dest.

Is this what’s really happening? And could somebody please explain to me why?

This is not really work related, I’m just trying to understand better how pointers behave.

Thank you very much for your replies!

  • 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-12T23:36:25+00:00Added an answer on June 12, 2026 at 11:36 pm

    The problem occurs here

    while (*dest++ = *src++)
        ;   
    

    which in plain english means copy the thing pointed to by src into the thing pointed to by dest, and after that increment both src and dest (that is, advance both pointers to point to the next thing).

    Use a copy of the pointer if you want to avoid changing it – eg the a variable you already created, and return that one.

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

Sidebar

Related Questions

I have a class with a custom indexer like so public string this[VehicleProperty property]
I have this custom textbox that I am working on and I can use
So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
I have a custom attribute that is being filled from a database. This attribute
I have an assignment that I need to create a custom C type string
I have extended std::string to fulfil my needs of having to write custom function
I have homework that I need to make a custom string class using C
I have a lot of strings containing custom tags: hello {RT|12} world {RT|465} this
I have this custom JSlider, which will be used in many other forms/windows. But
I have this custom validation attribute for validating a collection. I need to adapt

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.