Page 1 of 1
Strcpy
Posted: Sun Jan 18, 2004 9:52 am
by chris
How come when I use the strcpy function I get a segmentation fault? It's right out of Kernighan and Ritchie's book.
Code: Select all
#include <stdio.h>
void _strcpy(char *d, char *s);
int main(void)
{
char *s;
char *d;
printf("Enter String: ");
scanf("%s", &s);
_strcpy(d, s);
printf("%s\n", s);
printf("%s\n", d);
return 0;
}
void _strcpy(char *d, char *s)
{
while(*d++ = *s++)
;
}
Re:Strcpy
Posted: Sun Jan 18, 2004 10:08 am
by Candy
chris wrote:
How come when I use the strcpy function I get a segmentation fault? It's right out of Kernighan and Ritchie's book.
Code: Select all
#include <stdio.h>
void _strcpy(char *d, char *s);
int main(void)
{
char *s;
char *d;
printf("Enter String: ");
scanf("%s", &s);
_strcpy(d, s);
printf("%s\n", s);
printf("%s\n", d);
return 0;
}
void _strcpy(char *d, char *s)
{
while(*d++ = *s++)
;
}
- never mind, some sort of browser malfunction removed my comments. It was equal to the idea that you should allocate something to the pointer before using it in either strcpy or scanf. Also, try to use only strncpy, it doesn't allow for buffer overflows (and use something other than scanf, because it suffers from the same flaw).
Re:Strcpy
Posted: Sun Jan 18, 2004 11:47 am
by Neo
how about allocating some memory for the strings also?
Code: Select all
s=(char*)malloc(some_len);
d=(char*)malloc(some_len);
its safer that way.
Re:Strcpy
Posted: Sun Jan 18, 2004 12:40 pm
by Schol-R-LEA
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.
Re:Strcpy
Posted: Sun Jan 18, 2004 1:21 pm
by chris
Thanks, it works fine now. So I need to always allocate memory for a char pointer if I don't initialize it?
Re:Strcpy
Posted: Sun Jan 18, 2004 3:18 pm
by Tim
Yes.
A pointer is useless without something to point to. Just after you've declared a pointer, it's pointing nowhere. You have to point it somewhere: either to a block allocated using malloc (like Neo says), or to a non-pointer variable that you've declared yourself (such as an array, like Schol-R-LEA says).