Error on scanf with structure

This is a discussion on Error on scanf with structure within the C Programming forums, part of the General Programming Boards category; Hi there! Wright now I am on Structures (More Info on My Learning Diary on My Blog ) Why does ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32

    Error on scanf with structure

    Hi there! Wright now I am on Structures
    (More Info on My Learning Diary on My Blog)

    Why does the application crashes at this point? I've written the scanf form exactly like it is in the example in my book

    ---------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    Code:
    struct user
    {
           char name, surname;
           int age;
    };
    struct user database[10];
    
    void read_database()
    {
         int i,age;
         char name,surname;
         for (i=1; i<2; i++)
         {
             printf ("\nGive name for user %d ", i);
             scanf ("%s", database[i].name);
             
             printf ("\nGive surname for user %d ", i);
             scanf ("%c",database[i].surname);
             
             printf ("\nGive age for user %d ", i);
             scanf ("%d",database[i].age);         
         }
    }
    ---------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    &#37;s reads into a char[] (or a char *), but not a char.

    Edit: And I know your textbook uses & in scanf's where appropriate (into "plain" char and int variables).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    well even if i put c its just the same

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Lookup the syntax for declaring and using character strings in your book before going any further. Refer to your C API documentation for details on using scanf properly.



    ITSA
    Socket Library!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    I found that i should declare the structure this way

    Code:
    struct user
    {
           char name[10];
           char surname[10];
           int age;
    };
    why? Why should i declare the variables as arrays?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Why should i declare the variables as arrays?
    Because names consist of one or more characters. Of course, in this case you are restricting names to no more than 9 characters (the 10th is the null terminator).
    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

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    Its true.

    Initially i thought the same think but i saw that i could write more characters than 9.

    However with the latest test I see that you can write more characters, but it will store only 9

    Thats actually the scanf. You give order to scan the screen and store its content to that array, so it stores the most of the array's capacities.

    I'm beggining to learn! (lol)

    Its a miracle that it does not crash cause scanf should attempt "overstoring" the array - trying to store all elements scaned.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    However with the latest test I see that you can write more characters, but it will store only 9

    Thats actually the scanf. You give order to scan the screen and store its content to that array, so it stores the most of the array's capacities.
    Unless you provide the length, scanf() will just as much as it can into the string, possibly leading to a buffer overflow.

    Its a miracle that it does not crash cause scanf should attempt "overstoring" the array - trying to store all elements scaned.
    Miracles do happen, but when they don't and you assume they do, disasters happen. Consequently, use fgets() instead of scanf() for reading in a string, unless you want to specify the number of characters to be read in the format specifier for scanf().
    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

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    So whats actually the point of structures?

    They are used for creating databases?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    So whats actually the point of structures?
    To group related objects (variables) together into a data structure (or from a C++ish point of view, to create a user defined type).

    They are used for creating databases?
    Yes, but that is just an example, not their primary purpose.
    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

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    So their actually objects like the classes from C++ just more specified and restricted

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    So their actually objects like the classes from C++ just more specified and restricted
    Yes, though "restricted" in the sense that they lack native object oriented features that are present with the syntax of C++ classes and structs. However, read Stroustrup's glossary for the definitions of object.
    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

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> However with the latest test I see that you can write more characters, but it will store only 9

    Actually, no - using scanf in that way can lead to nasty buffer overflows. Instead:

    scanf("%9s", database[i].name);

    [edit]
    I guess I was slow to post.
    [/edit]
    Last edited by Sebastiani; 09-20-2008 at 11:54 AM.



    ITSA
    Socket Library!

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    This forces scanf to read only te first 9 characters?

    Handy!

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    This forces scanf to read only te first 9 characters?
    That is what I mean by "specify the number of characters to be read in the format specifier for scanf()". However, fgets() is somewhat easier to use since the number is characters is just specified in an argument.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. input from scanf into structure, how?
    By Elite in forum C Programming
    Replies: 8
    Last Post: 03-04-2005, 01:59 PM
  3. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

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