|
-
August 22nd, 2011, 10:19 AM
#1
Very basic question
This is pretty embarrassing... I've been coding in C++ off and on for 5 years now. I'm trying to run the following routine but getting a seg failt when I try to assign the 'f' . It doesn't matter if I try to assign it to str[0] or str[3] or if I try to assign 'o' or '7'. I also get the same problem if I declare str as char str[5].
Help?
int main(){
char* str;
str = new char[5];
str = "test";
cout << str << endl;
str[1] = 'f'; // causes seg fault
cout << str << endl;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|