Answer the question
In order to leave comments, you need to log in
How to write an analogue of the strcpy() function with memory allocation?
According to the task, you need to write a function, copying a string, into a new string with memory allocation. Why should it be allocated in this function?
Unfortunately, according to the code, nothing comes to mind. Google also gives inappropriate options.
This ... works, but to me, it's an unfortunate option.
char *str1 = "123456789112345";
char *str2 = "sdf";
str1 = str2;
cout << str1 << endl;
cout << str2 << endl;
Answer the question
In order to leave comments, you need to log in
you need to write a function, copying a string, to a new string with memory allocation
char* strdup( const char *s ) {
char *p = malloc( strlen( s ) + 1 );
if( p ) strcpy( p, s );
return p;
}
If you are using iostreams, work with std::string. If a teacher is interfering with C and C++ in front of the students, tell him he's weird.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question