简体   繁体   中英

How can I copy multiple char* strings into one using strcpy_s()?

Using the strcpy_s() function I want to collate the first three strings into the final one to print my full name. This is what I have and it doesn't work as I'm using char* strings and not std::string s.

#include <iostream>
using namespace std;

int main()
{
    char str_first[] = "Nerf_";
    char str_middle[] = " Herder";
    char str_last[] = "42";

    char str_fullName[35];

    strcpy_s(str_fullName, (str_first + str_middle + str_last).c_str());
    cout << str_fullName;
}

Any suggestions?

This should be close to what you're looking for, strictly using strcpy_s to concatenate strings together:

#include <string.h>
#include <iostream>
using namespace std;

int main()
{
    char str_first[] = "Nerf_";
    char str_middle[] = " Herder";
    char str_last[] = "42";

    char str_fullName[35];

    int index = strcpy_s(str_fullName, sizeof str_fullName, str_first);
    index += strcpy_s(str_fullName + index, sizeof str_fullName - index, str_middle);
    index += strcpy_s(str_fullName + index, sizeof str_fullName - index, str_last);
    cout << str_fullName;
}

The index variable serves a couple of purposes: (1) to provide a new index into the output str_fullName string as the string is built, and (2) subtracted from sizeof str_fullName , it "adjusts" the available buffer size as the string is built.

Caveats are that you should add overflow checking via the output from strcpy_s , and (as noted by others) there are better patterns to follow for doing this, but probably as an academic exercise there's something good to be learned here.

You need to use both strcat and strcpy

See code comments for more info.

// disable SDL warnings in Visual studio
#define _CRT_SECURE_NO_WARNINGS

#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    // TODO: insert checking code,
    // to make sure destination can hold all characters + one termination.

    char str_first[] = "Nerf_";
    char str_middle[] = " Herder";
    char str_last[] = "42";

    char str_fullName[35];

    // copy first string because we need null terminated destination
    strcpy(str_fullName, str_first);

    // append the rest, string is auto null terminated.
    strcat(str_fullName, str_middle);
    strcat(str_fullName, str_last);

    cout << str_fullName;
}

If I am not mistaken the function strcpy_s expects three arguments. So either supply one more argument to the function call or use instead the function strcpy .

And there is no need to use the standard class std::string to perform the task.

The code can look the following way

strcpy( str_fullName, str_first );
strcat( str_fullName, str_middle );
strcat( str_fullName, str_last );

Or you could use strcpy_s and strcat_s provided that you will specify the correct number of arguments.

Pay attention to that you need to include the header

#include <cstring>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM