While Neo is correct in that you need to have memory allocated for both s and d in order for this to work (I'd actually recommend changing the declarations of them to char arrays as the easier approach), this isn't the cause of the segfault. If you use a breakpoint at the _strcpy() function, you'll find that it faults before the function even runs. The real problem is in this line:
You see, s is already a pointer; by using the address operator, you are actually passing scanf() a **char, not a *char, which results in the access violation in scanf(). If you remove the '&', it runs fine (assuming you've allocated string memory as suggested above). This is the case even if s and d are declared as arrays, I might add. So if you modify the first several lines of main() like so:
Code: Select all
char s[32];
char d[32];
printf("Enter String: ");
scanf("%s", s);
it should work. I've tested these changes using Dev-C++ under WinXP, and it runs fine.
Lastly, I might add that strncpy() is generally preferable to strcpy(), as it helps prevent buffer overruns, though if you're implementing a library, you'd want to write both.