Thread: strcpy get errors

  1. #31
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    scanf("%s %s %s", str1, str2, str3);

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

    scanf("%[^;];%[^;];%[^;];", str1, str2, str3);
    This is not what i mean
    see my uploaded image below
    strcpy get errors-capture-jpg
    it is from source CODE here,
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define STR_MAX 100
    main()
    {char x[STR_MAX];
     printf("Your name  =");scanf("%s",x);
     printf("Your name is =%4s\n",x);system("pause");return EXIT_SUCCESS;}

  2. #32
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by loserone+_+ View Post
    This is not what i mean
    scanf is for "formatted input" which basically means that it assumes a delimiter of some sort. The default delimiter is whitespace, which is why it stops after "Unknown" in your example

    If you just want to get an entire line without regard to formatting, you're better to use fgets

    Code:
    char x[STR_MAX] = {'\0'};
        fgets(x, STR_MAX-1, stdin);
        printf("Your name is =%4s\n",x);

  3. #33
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Code:
    char x[STR_MAX] = {'\0'};
    
    fgets(x, STR_MAX-1, stdin);
    
    printf("Your name is =%4s\n",x);

    lol it works, great
    but is STR_MAX thing can be changed to numbers?
    and must in the back of fgets must write stdin a.k.a std input?

  4. #34
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by loserone+_+ View Post
    but is STR_MAX thing can be changed to numbers?
    and must in the back of fgets must write stdin a.k.a std input?
    STR_MAX is an integer (aka a number)
    fgets requires 3 parameters. The third parameter, stdin, is where you want to read from.

    Better to see the manual page for this function to find out more

    fgets(3): input of char/strings - Linux man page

  5. #35
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by loserone+_+ View Post
    Code:
    char x[STR_MAX] = {'\0'};
    
    fgets(x, STR_MAX-1, stdin);
    
    printf("Your name is =%4s\n",x);

    lol it works, great
    but is STR_MAX thing can be changed to numbers?
    and must in the back of fgets must write stdin a.k.a std input?
    I think you can use
    char gets (char *str)


    Code:
    char x[STR_MAX] = {'\0'};
     
    gets(x);
     
    printf("Your name is =%4s\n",x);

  6. #36
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

  7. #37
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Do NOT use "gets"
    Fact - Beethoven wrote his first symphony in C

  8. #38
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Well I would not have a problem using it really not unless I was writing a under interface for someone's life support system,
    if my program to swop two strings crashed it's not the end of the world.

    At the end of the day you can put all the safety precautions into your program you like but you can't stop the office cleaner unplugging the computer
    to plug in her hoover.
    Last edited by esbo; 01-15-2013 at 07:19 PM.

  9. #39
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I'd like to make 2 small but important points here.

    First, it's a bad idea to specify the string length when initializing an array with a string literal, Example:
    Code:
    char ar[5] = "Hello George";
    Note how 5 is not enough to hold the array, this is bad. That's why you should always declare it like this unless you are declaring a buffer of a certain
    size in which case you probably want to initialize to 0 not a string literal:
    Code:
    char ar[] = "Hello George"; /* compiler determines length automatically */
    
    /* a generic buffer */
    char buf[512] = {0}; /* this ensures that if i only fill lets say 10 bytes of this buffer, the rest of the buffer will always contain 0s and thus be a valid string */
    Second point, when using fgets you do not need to specify SIZE-1, fgets already does this for you and it simply results in odd problems (if you are for example using an exact size, which is quite often the case in my experience) or simply harder to read for no reason what-so-ever. Here is the preferred method again:
    Code:
    #define MAX_SIZE 100
    char ar[MAX_SIZE];
    ...
    fgets(ar, MAX_SIZE, fp);
    If you don't believe me, here's the excerpt from the fgets man page:
    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by
    s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating
    null byte ('\0') is stored after the last character in the buffer.

  10. #40
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by esbo View Post
    Well I would not have a problem using it really not unless I was writing a under interface for someone's life support system,
    if my program to swop two strings crashed it's not the end of the world.
    As long as your program interacts exclusively with trusted users that never make mistakes, and as long as your program never communicates directly or indirectly with any outside systems at all, then it's probably safe to use an unsafe function like gets().

    Buffer overflows are one of the most common cause for security vulnerabilities in software. If you use something like gets() you're basically guaranteeing that your program is vulnerable to this common vulnerability. It doesn't matter if it's running a life support or a game that's just for entertainment.

    But you're wrong about it being the end of the world. Eventually your program will get too much input and your gets() function will combine with poor judgment to cause a massive undefined operation. Imagine that your super-optimized code which relies on gets() is running in computers in the LHC, and the resulting undefined operation causes a chain reaction that causes a black hole to develop, causing the entire planet to collapse into it. If that happens, we'll know who to blame.

  11. #41
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well I would not have a problem using it really not unless I was writing a under interface for someone's life support system, if my program to swop two strings crashed it's not the end of the world.
    Your argument is technically valid - if it's a "pet" project that won't ever see the light of day, and is not important, then (in that case) there is no real harm in using "gets()". However, consider the following points:

    • Those who are relatively new to programming might not know about the problems associated with that function, and it is in their best interest to be informed about them.

    • The "gets()" function is depreciated - it's scheduled to be removed from the standard.

    • Why use an unsafe function if there is a safer alternative? It's good practice to be in the habit of using the better of the two. Even if the code you write never sees the light of day, if one day you do start writing critical code, it's one less thing to "unlearn."


    Of course, none of these points is intended to counter your main argument, and none of them really do. They're just some of the reasons why people stress the use of "fgets()" in lieu of "gets()." We want to give people the knowledge they need to write the best possible programs, and it's easy for them to use the better of those functions, for the reasons mentioned above.

    As an analogy, the chance of someone's house burning down is very very small. Does that mean it's wise to not change the batteries in your smoke detector?

    At the end of the day you can put all the safety precautions into your program you like but you can't stop the office cleaner unplugging the computer to plug in her hoover.
    Yes, but if it came to that, it would mean the difference between "you" (the programmer) or the janitor being held liable.

  12. #42
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    That's why you should always declare it like this unless you are declaring a buffer of a certain size in which case you probably want to initialize to 0 not a string literal

    I think that that is bad advice in this case -> Think about this example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    #define STR_MAX 16
     
     
    int main(void)
    {
        char a[]="Giovani";
        char b[]="Loserone+_+";
     
        strcpy(a,b);
    
    
        /* String a will not be big enough for string b */
    
    
        printf("Using strcpy:value of b, %2s\nUsing strcpy:value of a,%2s\n",b,a);
     
     
        system("pause");
     
     
        return EXIT_SUCCESS;
    }

    Well I would not have a problem using it really not unless I was writing a under interface for someone's life support system,
    if my program to swop two strings crashed it's not the end of the world.

    NEVER use "gets"


    In the future, you will only be able to compile a program using "gets" with a compiler which supports C89 or C99. C11 has taken "gets" out of stdio.h (C11 standard: Forward #6) and replaced it with gets_s (C11 K.3.5.4.1), where the maximum input is specified as an argument (Note that it recommends the use of fgets in the gets_s section C11 K.3.5.4.1 #6). This might not seem like a big deal now, but may be a problem in the future.


    Get to learn fgets as soon as you can. Don't think about "gets", learn how to make good programs with fgets ASAP - It's easy once you get it working once.

    Think of it this way: Don't you WANT to make a good program?


    ...but you can't stop the office cleaner unplugging the computer to plug in her hoover
    That's like saying, "I won't build a house strong enough for an Earthquake, because it may get hit with a comet".
    Fact - Beethoven wrote his first symphony in C

  13. #43
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by c99tutorial
    fgets requires 3 parameters. The third parameter, stdin, is where you want to read from.
    ohh, i see that
    Quote Originally Posted by c99tutorial
    Better to see the manual page for this function to find out more
    fgets(3): input of char/strings - Linux man page
    i will read the page later,
    Quote Originally Posted by esbo
    I think you can use char gets (char *str)
    fgets are from gets i think, so its basic of the gets reading,
    like fprintf from printf.
    Quote Originally Posted by Click_here
    Do NOT use "gets"
    Quote Originally Posted by Click_here
    NEVER use "gets"
    Quote Originally Posted by Matticus
    Using "gets()" is not a good idea -
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    it seems they who say that, they had a bad experience with gets() lol

    ok, enough of debating gets() and fgets(), back to first topic
    is that ok to using strcpy like this?
    Code:
    { char a[20],*s;
      printf("Insert String =");fgets(s,20,stdin);
      strcpy(a,s);
      printf("The Sentence A are,%5s",a);}

  14. #44
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by loserone+_+ View Post
    is that ok to using strcpy like this?
    Have you tried it?
    What result did you get?

    HInt: Where does "s" point to?

    Bye, Andreas

  15. #45
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Turn on warnings. Your compiler should warn you that s is used uninitialized, which probably means it's not right.

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