Hello!
I'm new in C++, and I have a basic question:
When I have a function:
void foo(str* s){
char* p;
p = new char[strlen(s)];
strcpy(p, s);
cout << s <<endl;
}
This function will take the length of s and in bytes and will allocate that much space in memory for p.
I want to do the same but ask the user to enter a string and then dynamicly allocate memory for the entered string, but it wouldnt compile for some reason, and i'd get a segmentation error. I know I can print s right away, but i want to allocate memory dynamicly in this exersize.
Here is the code I have so far:
int main(void){
char* s;
char* p;
cout << "Please Enter a string: ";
cin >> s;
p = new char[strlen(s)];
strcpy(p, s);
cout << p <<endl;
return 0;
}
Any help would be greatly appreciated.
Thank you!