Originally Posted by
kmdv
Because your code is not fine.
It worked because luckily the 'name' pointed to a valid memory location.
I would use this to refresh my memory on C strings....
so what would be the way to fix it?
to declare
char *name and immediately initialise it to a string?
like so:
Actually, I just used the debugger in Dev C++ and it didn't work, embarrassed here
I guess i would have to
Code:
char *name ="name" ;
initialize the pointer on declaration either to a hard coded string or I could do this
Code:
#include <iostream>
using namespace std;
int main(void)
{
char *name ;
char _name[10] ;
cout << "name: ";
cin >> _name ;
name = _name ;
cout << "your name is: " << name << "\n";
system("pause") ;
return 0;
}
And that should work.