Thread: strcpy get errors

  1. #16
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Code:
    char x[] = "hello";
    
    const char *y = "world";
    
    strcpy(x, y);
    must i using const char for strcpy?
    or i using const char for making repeat of sentence world?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by loserone+_+
    what is EXIT_SUCCESS do?
    Indicates that the program finished successfully, whatever that means to you. Returning 0 would have the same meaning.

    Quote Originally Posted by esbo
    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.
    Unless you are dealing with a freestanding implementation, or you are trying to re-interpret potentially inaccurate language in the text of the standard, the standard has always stated that the return type main shall be int.

    Quote Originally Posted by loserone+_+
    must i using const char for strcpy?
    No, but you should use const char* to point to the first character of a string literal. This makes it more likely that you will discover the mistake that you discovered in this thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    ok i finally understand all of your saying,
    thanks for advice , this thread really helping me

  4. #19
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    lol i thought i solved the problem at all,
    but when i am using scanf it getting program terminated,
    here's my code,
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define STR_MAX 100
    main()
    {char x[STR_MAX],y[STR_MAX];
     const char *r;
     printf("Your Name =");scanf("%2s",&r);
     strcpy(x,r);
     printf("Your Name Is =%2s",x);system("pause");return EXIT_SUCCESS;}
    whats wrong with scanf thing? must i using the thing -> & <- thing or not?

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    No, it's the same problem as before. You're not pointing at memory you can use(*).

    Code:
    char thisHasSpaceForAWord[100];
    char *thisPointsToTheSameSpace = thisHasSpaceForAWord;
    char *thisPointsToSomeOtherSpace = malloc(100);
    // These two do exactly the same thing, with the same block of memory
    scanf("%s",thisHasSpaceForAWord);
    scanf("%s",thisPointsToTheSameSpace);
    // This works with the allocated memory, not the locally declared array
    scanf("%s",thisPointsToSomeOtherSpace);
    Whenever you write a * in front of a variable, you need to also think about "OK, where is the memory coming from?".


    (*)
    OK, technically, &r is memory you can use, but it's total abuse of the concept, and you're stuck with very short strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by loserone+_+ View Post
    lol i thought i solved the problem at all,
    but when i am using scanf it getting program terminated,
    [/CODE]
    whats wrong with scanf thing? must i using the thing -> & <- thing or not?
    First, rewrite your program like this and then compile with warnings turned on

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STR_MAX 100
    int main(void)
    {
        char x[STR_MAX] = {'\0','\0','\0'};
        char y[STR_MAX] = {'\0','\0','\0'};
        const char *r;
        printf("Your Name =");
        scanf("%2s", &r);
        strcpy(x, r);
        printf("Your Name Is =%2s", x);
        system("pause");
        return EXIT_SUCCESS;
    }
    try.c: In function 'main':
    try.c:11:2: warning: format '%s' expects argument of type 'char *', but argument
    2 has type 'const char **' [-Wformat]

    What's the problem?
    1. The & will chang your variable r, which has type (const char *), to having type (const char **) which is wrong.
    2. r does not point to anything valid. Contrast this to x and y, which are as you can see pointing to arrays of '\0' characters.

    Possible fix:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STR_MAX 100
    int main(void)
    {
        char x[STR_MAX] = {'\0','\0','\0'};
        char y[STR_MAX] = {'\0','\0','\0'};
        char r[STR_MAX] = {'\0', '\0', '\0'};
        printf("Your Name =");
        scanf("%2s", r);
        strcpy(x, r);
        printf("Your Name Is =%2s", x);
        system("pause");
        return EXIT_SUCCESS;
    }

  7. #22
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    lol, funny thing is when i using
    Code:
    scanf("%2s",x)
    from the fixed code before,
    Code:
    #include<string.h>
    
    #define STR_MAX 100
    
    main()
    
    {char x[STR_MAX];
    
     const char *r;
    
     printf("Your Name =");scanf("%2s",&x);
    
     printf("Your Name Is =%2s",x);system("pause");return EXIT_SUCCESS;}
    It only print 2 character when program compiled
    so i change it to only
    Code:
    scanf("%s",x);
    and it works better,
    it seems i cant use strcpy from scanf pointer string like this
    Code:
    char x[20],*a;
    printf("Input Pointers =");scanf("%s",&a);
    strcpy(x,a);
    am i right? doesnt that just beat at all?

  8. #23
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Code:
    char x[STR_MAX] = {'\0','\0','\0'};
    char y[STR_MAX] = {'\0','\0','\0'};
    char r[STR_MAX] = {'\0', '\0', '\0'};
    lol i didnt know why i must wrote like that
    see my latest post, i fixed it but it seems i cant using scanf to pointer string like this
    Code:
    char x[20],*a;
    printf("Input Pointers =");
    scanf("%s",&a); strcpy(x,a);

  9. #24
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    When you write

    scanf("%s", FOO);

    FOO must have type (char *), not (const char *) and definitely not (char **).

    Code:
    char *a;
    scanf("%s", &a);   // WRONG: &a means it is now (char **)
    scanf("%s", a);     // OK: a is type (char *), but it doesn't point to anywhere valid.
    You must make sure a points to valid memory

    Code:
    char mybuffer[10000] = {'\0'};
    
    char *a = mybuffer; 
    scanf("%s", a);   // OK. a will not overflow as long as the input is no greater than 9999 characters, which is obviously not guaranteed.

  10. #25
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by loserone+_+ View Post
    Code:
    char x[STR_MAX] = {'\0','\0','\0'};
    lol i didnt know why i must wrote like that
    You don't have to write it like that. It is to make the point that you are setting aside some memory with '\0' filled in. By default, if you include ANY initializer for an array, then all of the other elements (all the way up to STR_MAX-1) are filled in with '\0'. However, if you don't specify an initializer, then the initial contents are undefined.

  11. #26
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    it seems, using scanf with pointer string
    it must had a hold with array,
    okay i understand, but what is the meaning of
    Code:
    char mybuffer[10000] = {'\0'};
    i am wanna know the
    Code:
    {'\0};
    thing do, what is the meaning of that?

  12. #27
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Normal array notation is like this

    Code:
    TYPE arrayname[SIZE] = {ELEM0, ELEM1, ELEM2, ...};
    So in your case TYPE is char, SIZE is 10000 and all the elements are '\0'. The character '\0' is just a special character that has numerical value zero. In C all valid strings must be terminated by this character.

    Code:
    char buffer[10000] = {'\0'};
    It means set aside a buffer of 10000 bytes, and initialize the buffer[0] to the value '\0'. The reason you want to explicitly initialize element 0 is following: If you forgot to initialize elements 1...9999, the compiler assumes you want to initialize the rest to 0, which is good.
    Last edited by c99tutorial; 01-15-2013 at 05:58 AM.

  13. #28
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    In C all valid strings must be terminated by this character.
    Code:
    char buffer[10000] = {'\0'};

    It means set aside a buffer of 10000 bytes, and initialize the buffer[0] to the value '\0'.
    the simple way u mean, it has zero value right?
    so it can hold 9999 value or 10000 value, thats ur mean right?

    ohh yeah, why scanf is reading for string didnt know the space bar character, so it will printing only characters. It didnt recognize space command? It is because the compiler or my mistake?

  14. #29
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by loserone+_+ View Post
    the simple way u mean, it has zero value right?
    so it can hold 9999 value or 10000 value, thats ur mean right?
    An array with size 10000 can hold 10000 values. The elements are numbered starting from 0. a[0], a[1], a[2], ..., a[9999]. If the array is a string, then the end of the string must be marked with '\0'. In other words, the longest string you can have is 9999 characters, because the longest string must have '\0' in the 10000th spot (the 10000th spot is called a[9999]).

  15. #30
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by loserone+_+ View Post
    ohh yeah, why scanf is reading for string didnt know the space bar character, so it will printing only characters. It didnt recognize space command? It is because the compiler or my mistake?
    This will read each one up to the whitespace character, so if you have a space in it, it will treat them as separate words:

    scanf("%s %s %s", str1, str2, str3);

    An easy way is to use a different delimiter, say ;

    scanf("%[^;];%[^;];%[^;];", str1, str2, str3);

    That means each string may contain any number of spaces, and they must be each terminated by a ';' character. The strings themselves may not contain ';' characters.

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, 11: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, 07:02 AM
  5. strcpy
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2002, 01:39 PM