Strcpy

Programming, for all ages and all languages.
Post Reply
chris

Strcpy

Post 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++)
      ;
}

User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Strcpy

Post 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).
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Strcpy

Post 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.
Only Human
Schol-R-LEA

Re:Strcpy

Post 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:

Code: Select all

   scanf("%s", &s); 
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.
chris

Re:Strcpy

Post by chris »

Thanks, it works fine now. So I need to always allocate memory for a char pointer if I don't initialize it?
Tim

Re:Strcpy

Post 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).
Post Reply