Strcpy not being recognized - 'not declared'

Hello, I am trying to execute the following requirement. Note I try to use 'strcpy' but I am getting an error. I am not sure if this could be a problem with my compiler (onlinegdb.com) not recognizing 'strcpy'? I attempted to include library <stdio.h> but it was not accepted. This is C++. Error "strcpy not declared".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*Exercise requirement:
  Create a function that dynamically allocates memory for a string
  with up to 256 characters using(use new keyword). Copy your name
  into the string. Make sure there is no memory leak.
*/

#include <iostream>
#include <string>

void createstr() {
    char * s = new char[256]; 
    strcpy(s, "myname");
    delete s;
    
}

int main()
{
    createstr();
    return 0;
}
strcpy is found in the header file <cstring>

Note that if you're going to use c-style strings, to make one with enough space for 256 characters, you need it to be of size 257, because a c-string marks the end of the string with a zero.

Your function does do what's required, although it also seems a bit useless, since it destroys the string as well. Still, it does do what the exercise requirement said.
Last edited on
Thank you. It works with 'cstring' library. I guess the documentation I was reading talking about <stdio> was only applied to C programming.
In C (and in C++ as well as an alternative), it's in the header file string.h
Topic archived. No new replies allowed.