Proper way to copy C strings

前端 未结 4 993
野的像风
野的像风 2020-12-02 00:18

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

4条回答
  •  自闭症患者
    2020-12-02 00:19

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

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

    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 
    
    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);
    

    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 
    
    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);
    

    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.

提交回复
热议问题