Re: replace gets

Discussion in 'C Programming' started by LibraryUser, Aug 28, 2003.

  1. LibraryUser

    LibraryUser Guest

    amanayin wrote:
    >
    > I have modified a program that i did not write the modifiaction
    > is as follows:
    >
    > original function
    > void getdata(char *n, char *a1, char *a2, char *a3)
    > {
    > printf("Enter name: ");
    > gets(n);
    > printf("Enter address 1: ");
    > gets(a1);
    > printf("Enter address 2: ");
    > gets(a2);
    > printf("Enter address 3: ");
    > gets(a3);
    > }

    .... snip ...
    >
    > it works but is there an easier way to do it whole program below


    The easiest way IMO is to use ggets (at
    <http://cbfalconer.home.att.net/download/ggets.zip> )

    and then your function becomes:

    void getdata(char *n, char *a1, char *a2, char *a3)
    {
    printf("Enter name: "); fflush(stdout);
    ggets(n);
    printf("Enter address 1: "); fflush(stdout);
    ggets(a1);
    printf("Enter address 2: "); fflush(stdout);
    ggets(a2);
    printf("Enter address 3: "); fflush(stdout);
    ggets(a3);
    } /* getdata */

    You will have to #include "ggets.h" and link in ggets.o etc. You
    should also remember to free the various strings when done with
    them, since ggets assigns the memory needed via malloc.

    The fflush statements are needed to ensure that the prompt is
    output before the input function is executed. This also applies
    to your original.

    You can also check the return value of ggets. Non-zero means EOF
    or error.

    --
    Replies should be to the newsgroup
    cbfalconerX-Mozilla-Status: 0009- Thu Aug 28 09:17:08 2003
    X-Mozilla-Status: 0801
    X-Mozilla-Status2: 00000000
    FCC: /C|/Program Files/Netscape/Users/default/mail/sentnews
    X-Mozilla-News-Host: news.msln.net
    Message-ID: <>
    Date: Thu, 28 Aug 2003 09:17:07 -0400
    From: LibraryUser <>
    Organization: Lincoln Memorial Library
    X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0;
    uuencode=0; html=0; linewidth=0
    X-Mailer: Mozilla 4.7 [en]C-NSCPCD (Win98; U)
    X-Accept-Language: en
    MIME-Version: 1.0
    Newsgroups: comp.lang.c,comp.programming
    Followup-To: comp.programming
    Subject: Re: Data structure
    References: <>
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit

    Peter wrote:
    >
    > I am looking for what may be a good data structure to use for
    > a given problem.
    >
    > I have to randomly insert,delete and lookup some objects - all
    > three being equally probable. The number of objects is in the
    > thousands. The keys are non integers ( but I can probably hash
    > them to integers if need be).
    >
    > To start with I have used a hash based approach. A lot of people
    > where I work use splay trees but I don't think that would be a
    > good choice in my situation


    You are OT for c.l.c, but comp.programming would be suitable. At
    any rate, my hashlib library will handle the operations you
    describe very nicely. See:

    <http://cbfalconer.home.att.net/download/>

    FUPs set to comp.programming

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation
     
    LibraryUser, Aug 28, 2003
    #1
    1. Advertising

  2. LibraryUser

    Simon Biber Guest

    "LibraryUser" <> wrote:
    > The easiest way IMO is to use ggets (at
    > <http://cbfalconer.home.att.net/download/ggets.zip> )
    >
    > and then your function becomes:
    >
    > void getdata(char *n, char *a1, char *a2, char *a3)
    > {
    > printf("Enter name: "); fflush(stdout);
    > ggets(n);
    > printf("Enter address 1: "); fflush(stdout);
    > ggets(a1);
    > printf("Enter address 2: "); fflush(stdout);
    > ggets(a2);
    > printf("Enter address 3: "); fflush(stdout);
    > ggets(a3);
    > } /* getdata */


    What? ggets expects a pointer to pointer to char. And it is
    not reasonable to just ggets(&n); etc, because how will the
    changed value of the variables n, a1, a3, a3 be returned to
    the caller of getdata?

    Make it:
    void getdata(char **n, char **a1, char **a2, char **a3)
    {
    printf("Enter name: "); fflush(stdout);
    ggets(n);
    printf("Enter address 1: "); fflush(stdout);
    ggets(a1);
    printf("Enter address 2: "); fflush(stdout);
    ggets(a2);
    printf("Enter address 3: "); fflush(stdout);
    ggets(a3);
    } /* getdata */

    But then of course the caller of getdata must be modified
    to send the addresses of 'pointer to char' variables,
    rather than allocate the arrays itself.

    --
    Simon.
     
    Simon Biber, Aug 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    5
    Views:
    550
    Steven Saunderson
    Jul 10, 2006
  2. Brian Blais
    Replies:
    1
    Views:
    407
    Bruno Desthuilliers
    Jun 27, 2006
  3. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    380
    John Joyce
    Mar 26, 2007
  4. John Joyce

    Return of gets gets

    John Joyce, Apr 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    215
    John Joyce
    Apr 23, 2007
  5. libsfan01
    Replies:
    5
    Views:
    273
    Jeff North
    Dec 20, 2006
Loading...

Share This Page