Re: what's wrong with this code from a book?

From: dandelion (dandelion_at_meadow.net)
Date: 11/22/04


Date: Mon, 22 Nov 2004 15:00:09 +0100


"ben" <x@x.com> wrote in message news:221120041314356269%x@x.com...
> In article <41a1d2d2$0$157$e4fe514c@dreader11.news.xs4all.nl>,
> dandelion <dandelion@meadow.net> wrote:
> > "ben" <x@x.com> wrote in message news:221120041136185563%x@x.com...
> ...
> > Check argc instead, which will give you the number of items in argv[].
> > argv[0] (by convention?POSIX) should give you the command that started
your
> > program.
> >
> > > thanks, ben.
> >
> > My pleasure.
> >
>
> great. i kind of knew all that but wasn't completely sure -- needed
> confirming -- so thanks very much for the clarification.
>
> still confused on part of it though -- this part in the book about data
> used by the below code: "... (randomly generated or read from standard
> input) ...". what would the command line call look like that makes the
> below programme randomly generate the data do you think? (in fact i'm
> not even sure about the command when feeding it data.
> ./a.out 4 2 4 6 8
> seems a reasonable guess, the first number saying how many numbers
> follow it, but i get useless result from that but as i said previously
> i think that maybe a problem in the other code modules so that command
> line might be right)
>
> so, all that in short, how do you think this programme is supposed to
> be called (to supply data, and make it randomly generate it)?
>
> (the type of data that the support files are using are ints, but as i
> say there's possibly something wrong with those files but before i
> tackle those i want to make sure this part is as it should be)
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include "item.h"
> #include "st.h"
>
> int main(int argc, char *argv[])
> {
> if(argc < 3)
      {
> fprintf(stderr, "Insufficient arguments\n"); exit(1);
      }

     The curly-braces *do* belong there and a semicolon ';' marks the end of
a statement, not a comma.

> int n, maxn = atoi(argv[1]), sw = atoi(argv[2]);
> Key v;
> Item item;

The declarations go *before* the first statement.

> stInit(maxn);

> for( n = 0; n < maxn; n++ ) {
> if( sw )
> v = itemRand();
> else if( itemScan(&v) == EOF )
> break;
> if( stSearch(v) != NULL )
> continue;
> key(item) = v;
> stInsert(item);
> }
> stSort(itemShow);
> printf("\n%d keys %d distinct keys\n", n, stCount() );
> return 0;
> }

So that would become

int main(int argc, char* argv[])
{
  int n, maxn;
  Key v;
  Item item;

  if(argc < 3)
  {
    /* no command line args given */
    fprintf(stderr, "Insufficient arguments, using random values\n");
    maxn = random() * WHATEVER_MAXN/MAX_RAND;
    sw = random() * WHATEVER_SW/MAX_RAND;
  }
  else
  {
    /* command line args given */
    maxn = atoi(argv[1]);
    sw = atoi(argv[2]);
  }

  /* at this point maxn and sw ae defined either way */
  for( n = 0; n < maxn; n++ ) {
       if( sw )
          v = itemRand();
       else if( itemScan(&v) == EOF )
          break;
       if( stSearch(v) != NULL )
          continue;
       key(item) = v;
       stInsert(item);
    }
    stSort(itemShow);
    printf("\n%d keys %d distinct keys\n", n, stCount() );
    return 0;
}

The above will *still* break if non-numeric arguments are provided, btw. If
you want to handle that aswell, use sscanf instead of atoi and check the
results.



Relevant Pages

  • [RFC: 2.6 patch] remove the broken SCSI_ACORNSCSI_3 driver
    ... * Abandoned using the Select and Transfer command since there were ... Once debugged, remove the #undef, otherwise to debug, ... -unsigned int dmac_address ... * Purpose: differentiate between commands that have a DATA IN phase ...
    (Linux-Kernel)
  • [RFC: 2.6 patch] remove drivers/net/eepro100.c
    ... This patch removes the obsolete drivers/net/eepro100.c driver. ... -static int rx_copybreak = 200; ... Each Tx command block ... -static void speedo_resume(struct net_device *dev); ...
    (Linux-Kernel)
  • RFC: [2.6 patch] small IPMI cleanup
    ... The patch below does the following changes to the IPMI code: ... * Send a command request from the given user. ... * the message response comes back, the receive handler for this user ... static int ipmi_init_msghandler; ...
    (Linux-Kernel)
  • [PATCH] Cleanups for the IPMI driver
    ... off the shutdown of the timer when removing the module. ... static int ipmi_init_msghandler; ... * Send a command request from the given user. ... * the message response comes back, the receive handler for this user ...
    (Linux-Kernel)
  • [PATCH, RFC] hide EH backup data outside the scsi_cmnd
    ... Currently struct scsi_cmnd has various fields that are used to backup ... This means drivers can easily get at it and misuse it. ... int retry_cnt = 1, rtn; ... Immediately prior to retrying a command, ...
    (Linux-Kernel)