hopefully simple gets() help

This is a discussion on hopefully simple gets() help within the C Programming forums, part of the General Programming Boards category; Good evening everybody, I'm after a little help if you can. I'm writing a lab report (uni) which involves analyzing ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    hopefully simple gets() help

    Good evening everybody,

    I'm after a little help if you can. I'm writing a lab report (uni) which involves analyzing a C program.

    A global variable is defined as:
    Code:
    char buffer[11];
    which is then used in main in:
    Code:
    printf ("\nPress a Key to Start:\n");
    
    gets(buffer,10);
    I'm assuming this is just a device to get the program to start running when the user wants it to, the thing that's confusing me is what ',10' means in the gets(buffer,10).
    I haven't been able to find any mention of anything like in my textbooks or around the internet, so I really hope someone can help.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    I thought the prototype for gets is char *gets(char *s)? How come you can call it like that? Are you sure your program compiles? I'd like to know what the experts on here says about this. More new things I can learn.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>gets(buffer,10);

    This is wrong, gets does not take a size argument. Fanoliv gave
    the correct prototype.

    Shouldn't use gets in any case, its old and dangerous - see my
    sig for the reasons, and also the better fgets function. If your
    university is telling you to use gets you're in trouble IMO - who
    knows what other bad code they're teaching.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    I realise gets is bad, that's the one piece of information I've been finding everywhere.

    This code was written for us and given to us to use in lab. We compiled it and downloaded it to a microcontroller. Running the program would cause "Press a Key to Start:" to be displayed on the PC user interface, a key would be pressed, and the rest of the program would run fine.
    As far as I could tell the whole program worked fine.

    There was another program which contained
    Code:
    char buffer[11];
    as a global variable declaration and
    Code:
    printf ("\nPress a Key to Start:");
    gets(buffer,2);
    in main, and this also appeared to work fine.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    The only thing I can think of is that maybe you are compiling with
    a C++ compiler which has an overloaded version of gets unique
    to that compiler, hence an additional parameter. In any case, it's
    not the best way to wait for a keypress, a simple getchar would
    suffice IMO, something like this is best though:

    Code:
    int ch;
    printf ("Press enter to continue");
    
    while ((ch = getchar ()) != '\n' || ch != EOF); /*in case they decide to type some junk before hitting enter!*/
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    I reckon you're probably right about the compiler, hadn't thought of that but it seems like the most likely reason, its one I've not used before, and will probably never use again.
    In the end its not a critical problem, but thanks for the help anyway

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You probably didn't include <stdio.h>, and hence didn't have the prototype for gets(), so the compiler couldn't tell you were passing the wrong arguments to it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Hmmm, never encountered that before. I've forgotten to include
    headers before, but never executed the code, always thought
    it wouldn't run...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > confusing me is what ',10' means in the gets(buffer,10).
    1. It means you didn't prototype the function, so the compiler lets you pass anything to the function. Since gets() only expects 1 parameter, anything else just wastes time and stack space.
    2. gets() is the world's worst function as already noted several times. Get used to using fgets() as soon as possible.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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