strcpy

hello,

i have a question about the strcpy-function.

the strcpy-function returns a pointer-to-char. Must you write a variabele of the type pointer to char , an assignment-operator, and then the strcpy-function?
Or can you just write the strcpy-function?
You can just write the strcpy function.
ok, but why isn't it a void-function?
Sometimes it makes it easier to write nested calls, it returns it's first (destination) argument.
Example :
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstring>

int main() {
    const char *str1 = "ha";
    char str2[3];
    std::strcpy(str2, str1)[1] = 'e';
    std::cout << str1 << '\n';
    std::cout << str2 << std::endl;
}

(sorry I couldn't come up with anything better)
R0mai, I noticed you used std::strcpy(str2, str1);.

Is strcpy in the std namespace? I've just tried it and it works with and without the std::
It's in both namespaces. <cstring> includes <string.h> and then pulls all the functions to the std namespace. I THINK using the non-std:: version is deprecated but it has EXACTLY the same behavior.

Ok, i've got it, thanks!
Last edited on
Topic archived. No new replies allowed.