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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:50:30+00:00 2026-05-30T21:50:30+00:00

Is there an easy way to copy C strings? I have const char *stringA

  • 0

Is there an easy way to copy C strings?

I have const char *stringA, and I want char *stringB to take the value (note that stringB is not const). I tried stringB=(char*) stringA, but that makes stringB still point to the same memory location, so when stringA later changes, stringB does too.

I’ve also tried strcpy(stringB,stringA), but it seems that if stringB wasn’t initialized to a large enough array, there’s a segfault. I’m not super experienced with C strings though, am I missing something obvious?

If I just initialize stringB as char *stringB[23], because I know I’ll never have a string longer than 22 characters (and allowing for the null terminator), is that the right way? If stringB is checked for equality with other C-strings, will the extra space affect anything?

(And just using strings isn’t a solution here, as I need minimal overhead and easy access to individual characters.)

  • 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-30T21:50:32+00:00Added an answer on May 30, 2026 at 9:50 pm

    You could use strdup() to return a copy of a C-string, as in:

    #include <string.h>
    
    const char *stringA = "foo";
    char *stringB = NULL;
    
    stringB = strdup(stringA);
    /* ... */
    free(stringB);
    stringB = NULL; 
    

    You could also use strcpy(), but you need to allocate space first, which isn’t hard to do but can lead to an overflow error, if not done correctly:

    #include <string.h>
    
    const char *stringA = "foo";
    char *stringB = NULL;
    
    /* you must add one to cover the byte needed for the terminating null character */
    stringB = (char *) malloc( strlen(stringA) + 1 ); 
    strcpy( stringB, stringA );
    /* ... */
    free(stringB);
    stringB = NULL;
    

    If you cannot use strdup(), I would recommend the use of strncpy() instead of strcpy(). The strncpy() function copies up to — and only up to — n bytes, which helps avoid overflow errors. If strlen(stringA) + 1 > n, however, you would need to terminate stringB, yourself. But, generally, you’ll know what sizes you need for things:

    #include <string.h>
    
    const char *stringA = "foo";
    char *stringB = NULL;
    
    /* you must add one to cover the byte needed for the terminating null character */
    stringB = (char *) malloc( strlen(stringA) + 1 ); 
    strncpy( stringB, stringA, strlen(stringA) + 1 );
    /* ... */
    free(stringB);
    stringB = NULL;
    

    I think strdup() is cleaner, myself, so I try to use it where working with strings exclusively. I don’t know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.

    Note that I cast the result of malloc() to char *. This is because your question is tagged as a c++ question. In C++, it is required to cast the result from malloc(). In C, however, you would not cast this.

    EDIT

    There you go, there’s one complication: strdup() is not in C or C++. So use strcpy() or strncp() with a pre-sized array or a malloc-ed pointer. It’s a good habit to use strncp() instead of strcpy(), wherever you might use that function. It will help reduce the potential for errors.

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

Sidebar

Related Questions

Is there an easy way to copy a nested Array so that every object
Is there an easy way to copy all stored procedures from one database to
I am wondering is there an easy way to make a copy of a
Is there an easy way to set up a system which will copy data
Is there an easy way to copy a project in Visual Studio 2010 ?
Is there an easy way to copy the properties of one object into another
Is there an easy way to iterate over an associative array of this structure
Is there an easy way in C# to create Ordinals for a number? For
Is there an easy way to produce MSDN-style documentation from the Visual Studio XML
Is there an easy way to avoid dealing with text encoding problems?

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.