Like Tree40Likes

strcpy get errors

This is a discussion on strcpy get errors within the C Programming forums, part of the General Programming Boards category; the program i make had stopped working, it has strcpy, did i go wrong? here's my code .... Code: #include<stdio.h> ...

  1. #1
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112

    strcpy get errors

    the program i make had stopped working,
    it has strcpy,
    did i go wrong?
    here's my code ....
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    main()
    {char *a,*b,*c,*d;
          a="Giovani";b="Loserone+_+";c="Geofff";d="Loserone4RA";
          strcpy(b,a);printf("Using strcpy:value of b, %2s\nUsing strcpy:value of a,%2s\n",b,a);
          strcpy(d,c);printf("Using strcpy:value of d, %2s\nUsing strcpy:value of c,%2s\n",d,c);
          system("pause");}
    i just want to know, what the strcpy do in string, but when compile and run its just stopped working, i dont know why,
    is something wrong with that code, please tell me what my wrong is?

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I think you are declaring your strings as fixed constants not variables.

    You will do better with for example

    Code:
    char a[20]="whatever";
    (if that compiles lol)

    You see 'a' is now space for a variable, before it was a string you can't change
    because I think it was a string constant which is something that the compile won't you change


    more to the point (pun intended) 'a' is a string pointed, so I think you could do what you want by saying

    simple

    Code:
    a=b; //!!!!!!!
    then have a temporary string points t*

    so

    Code:
    t=a;
    a=b;
    b=t;
    Might work!! But I am not great working with strings myself
    Last edited by esbo; 01-14-2013 at 10:30 PM.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    You are declaring a,b,c,d as pointers.

    If you declare them as string literals, you may not modify them.

    You will need to declare them as arrays large enough to hold the biggest string.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    int main(void)
    {
      char a[16]="Giovani", b[16]="Loserone+_+", c[16]="Geofff", d[16]="Loserone4RA";
      ...
    
      return 0;
    }
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Code:
    char a[20]="whatever";
    so u mean i must using string with array not with pointer?
    what do u mean a fixed constants?
    is that
    Code:
    char *a,*b,*c,*d;
    not a variable?
    and all i want to know is using strcpy on string,

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by loserone+_+ View Post
    Code:
    char a[20]="whatever";
    so u mean i must using string with array not with pointer?
    what do u mean a fixed constants?
    is that
    Code:
    char *a,*b,*c,*d;
    not a variable?
    and all i want to know is using strcpy on string,
    Read my updated post. I am not sure what you want to do, you can effectively do a copy by changing what the pointers point to.
    That is more efficient than moving the whole string.

    It's like moving a big file to a new folder on a hard drive, the file does not move at all, the name and the pointer to the file move to the new folder
    the file stays where it is (unless you move to a folder on a different drive)..




    Or for example if you had

    int a, b;

    you can say
    a=5;
    b=6;
    a=b.

    But you can't say 5=6 because 5 and 6 are treated as constants and I think your strings are treated as constants in a similar way.

    I would say I am not 100% sure of this because I have never tried (anything so stupid lol - no offence intended )
    Last edited by esbo; 01-14-2013 at 10:42 PM.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    also why use %2s? that only prints 2 character, use just %s.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    This is what I meant - And I neatened up your code. Enjoy.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define STR_MAX 16
    
    
    int main(void)
    {
        char a[STR_MAX]="Giovani";
        char b[STR_MAX]="Loserone+_+";
        char c[STR_MAX]="Geofff";
        char d[STR_MAX]="Loserone4RA";
    
    
        strcpy(b,a);
        printf("Using strcpy:value of b, %2s\nUsing strcpy:value of a,%2s\n",b,a);
    
    
        strcpy(d,c);
        printf("Using strcpy:value of d, %2s\nUsing strcpy:value of c,%2s\n",d,c);
    
    
        system("pause");
    
    
        return EXIT_SUCCESS;
    }
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Click_here View Post
    This is what I meant - And I neatened up your code. Enjoy.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define STR_MAX 16
    
    
    int main(void)
    {
        char a[STR_MAX]="Giovani";
        char b[STR_MAX]="Loserone+_+";
        char c[STR_MAX]="Geofff";
        char d[STR_MAX]="Loserone4RA";
    
    
        strcpy(b,a);
        printf("Using strcpy:value of b, %2s\nUsing strcpy:value of a,%2s\n",b,a);
    
    
        strcpy(d,c);
        printf("Using strcpy:value of d, %2s\nUsing strcpy:value of c,%2s\n",d,c);
    
    
        system("pause");
    
    
        return EXIT_SUCCESS;
    }

    Yes that looks fine to me. Before you just declared pointers, this time declared memory space that can be copied to.
    Last edited by esbo; 01-14-2013 at 11:01 PM.

  9. #9
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    You are declaring a,b,c,d as pointers.

    If you declare them as string literals, you may not modify them.

    You will need to declare them as arrays large enough to hold the biggest string.
    It works when i am using strcpy with array,
    so this means strcpy didnt useful with pointers string? am i right?

    I would say I am not 100% sure of this because I have never tried (anything so stupid lol - no offence intended )
    Yes this is stupid , but i had to learn it because this is basic of c programming

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    I wouldn't worry about that mistake -> It is quite common for people just learning C to get arrays and pointers mixed up.

    Also note how I've declared main as "int main" - And I have given it a return value "EXIT_SUCCESS" (this value is defined in stdlib.h). This is very important and you should take note.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by loserone+_+
    so this means strcpy didnt useful with pointers string? am i right?
    No, this means that you should not attempt to copy to a string literal (constant) because attempting to change a string literal results in undefined behaviour.

    Hence, it is fine to write:
    Code:
    char x[] = "hello";
    char y[] = "world";
    strcpy(x, y);
    or:
    Code:
    char x[] = "hello";
    const char *y = "world";
    strcpy(x, y);
    but not:
    Code:
    char *x = "hello";
    const char *y = "world";
    strcpy(x, y);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    also why use %2s? that only prints 2 character, use just %s.
    %2s, %3c, %4i, it gives more spacing when printing variables,
    u dont know that?

    okay, what's the difference between
    Code:
    main
    with
    Code:
    int main
    and what is EXIT_SUCCESS do?

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by loserone+_+ View Post
    It works when i am using strcpy with array,
    so this means strcpy didnt useful with pointers string? am i right?


    Yes this is stupid , but i had to learn it because this is basic of c programming
    Only joking!! I have done far more stupid thing in C I can assure you!!!
    Although I have done quite a lot of things in C programming I am still unsure of some of the basics because I was never formally taught C
    I just learned it myself and once I found a way that worked I just stuck with what I knew worked.
    I have never declared a string literal such as.

    Code:
    char a[STR_MAX]="Giovani";
    I would just go

    Code:
    char a[16];
    strcpy( a, "Giovani");
    Problem for me is when I look at other peoples code which use things
    I do not use.

    So I tend to use a small subset of C which I understand (fairly) well lol. So I would never get your problem because I don't need to initialise variable in a way I am unfamiliar with, so I don't.

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Sorry I am not sure I never do that kind of stuff!!

    EXIT_SUCCESS is probably defined as zero, you can check with a printf"%d",EXIT_SUCCESS);

    I think main is an int by default but I am not sure, they changed the standard at some point and it might be a void so I don't
    know, it might be different on different compilers.

    I never use it anyway. I would prefer to use return 0 because I know what a 0 is.
    EXIT_SUCCESS could be defined as anything!! probably 0 or 1 though.

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Sorry I am not sure I never do that kind of stuff!!

    EXIT_SUCCESS is probably defined as zero, you can check with a printf"%d",EXIT_SUCCESS);

    I think main is an int by default but I am not sure, they changed the standard at some point and it might be a void so I don't
    know, it might be different on different compilers.

    I never use it anyway. I would prefer to use return 0 because I know what a 0 is.
    EXIT_SUCCESS could be defined as anything!! probably 0 or 1 though.

Page 1 of 9 123456789 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using strcpy
    By laxkrzy in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 10:09 PM
  2. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  3. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  4. strcpy
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 06:02 AM
  5. strcpy
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2002, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21