for certain functions i want to create a copy of the string within the function and then manipulate this - for some strange reason, i cant get strcpy to work (gives me a segmentation fault) - i've also tried passing the arg as a string, this doesnt work either (g++ throws an error saying it expect a char*)
#include <iostream>
#include <cstring>
using namespace std;
void copy_string(char* stri);
int main ()
{
copy_string("sample string");
return 0;
}
void copy_string(char* stri) {
char* stri_copy;
strcpy(stri_copy, stri);
cout << "String: " << stri_copy;
}
im not sure i understand why this is happening.
so my two questions are:
- why this is occuring - is there an easy fix?
- whats the simplest / most efficient way of creating a local copy of a string passed to a function?
thanks!