0

I'm trying to use strcpy() with pointers to strings and after a successful compilation when I run it it gives error. I don't know why this is happening.

int main()
    {
         char *s1="abcd";
         char *s2="efgh";

         strcpy(s2,s1);

         printf("%s\n", s1);
         printf("%s\n",s2);

         getch();
    }
  • The real question here is, why are you trying to call `strcpy` at all? You probably heard that you had to, but *that is for strings that you are storing in arrays*. If your strings are stored in arrays, you can't use `=` to assign them, you have to use `strcpy`. But if you have strings that are represented as `char *`, the `=` operator works just fine. – Steve Summit Dec 02 '19 at 17:09
  • Are you trying to do combine these char* strings – Kalana Dec 02 '19 at 18:01
  • 1
    @Kalana No I'm just trying to copy s1 string to s2 string... – Abhirup Bakshi Dec 02 '19 at 18:03
  • @AbhirupBakshi If this is an assignment, the answer you accepted is probably not what your professor wanted you to do. I'm guessing that your professor wanted you to make the string modifiable by placing it on the stack, which is detailed in my answer. – S.S. Anne Dec 03 '19 at 00:08

2 Answers2

4

These are string literals, you can't modify them because they're stored in read-only memory.

If you want to change this so you can modify them, use char s[]. This will store the strings on the stack:

         char s1[] = "abcd";
         char s2[] = "efgh";

If you want pointers to these, simply create pointers:

         char *p1 = s1;
         char *p2 = s2;

or you can create them with compound literals from C99:

         char *p1 = (char []){"abcd"};
         char *p2 = (char []){"efgh"};

A full program that puts the strings on the stack:

int main(void)
{
     char s1[] = "abcd";
     char s2[] = "efgh";

     strcpy(s2, s1);

     printf("%s\n", s1);
     printf("%s\n", s2);

     getchar();
}

Output:

abcd
abcd
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • @JL2210 And what is the difference between string literals and normal strings – Abhirup Bakshi Dec 02 '19 at 17:25
  • @AbhirupBakshi I've made an edit that should address your concerns. The difference between string literals and normal strings is negligible; they can be used the same. – S.S. Anne Dec 03 '19 at 00:03
1

You are trying to copy all content from first pointer string to second pointer string then I like to suggest you to use malloc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv) {

      char *s1 ="abcd";
      char *s2 ="efgh";

      s2 = (char *) malloc(1 + strlen(s1));

      strcpy(s2, s1);

      printf("%s\n", s1);
      printf("%s\n", s2);

      return 0;
}
output -:abcd                                                                                                                
         abcd 

hope this will fulfill your question

Kalana
  • 5,631
  • 7
  • 30
  • 51
  • It's much easier to allocate memory on the stack than with `malloc`, especially with small allocations. You don't have to `free` the memory if you do that (which you forgot to do). – S.S. Anne Dec 03 '19 at 00:06