Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #109871

Re: What am I doing wrong here?

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: What am I doing wrong here?
Date 2017-05-13 06:55 +0000
Organization Freie Universitaet Berlin
Message-ID <ennoubFn8ntU1@mid.uni-berlin.de> (permalink)
References <of62cn$4ji$3@dont-email.me>

Show all headers | View raw


DFS <nospam@dfs.com> wrote:
> Just trying to get some random chars and ints:

> ---------------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <time.h>

> int main(int argc, char *argv[])
> {
>    int i;
>    char *randName;
>    int  randRate;
>    char *randJob;

>    char *names[] = {"A","B","C","D","E"};
>    int  rates[]  = {10,20,30,40,50};
>    char *jobs[]  = {"programmer","DBA","analyst","architect","contractor"};

>    int namescount = sizeof(names) / sizeof(names[0]);
>    int ratescount = sizeof(rates) / sizeof(rates[0]);
>    int jobscount  = sizeof(jobs)  / sizeof(jobs [0]);

>    for(i=0;i<atoi(argv[1]);i++)
>    {
>         srand(time(NULL));
>         sprintf(randName ,"%s", names[rand()%namescount]);

Already this line is broken like all the others where you
use sprintf(). sprintf() tries to put a string into the
memory pointed to by the first argument. For that to work
the first argument must be a (pointer to a) char array
large enough to hold the complete string sprintf() will
try to put into it. But what you pass it here is an un-
initilaozed pointer, thus pointing to some random place
in memory. If you're lucky that will crash your program
on the first attempt with a segmentation fault, but if you
are unlucky it will seem to work but will corrupt the
 memory of your program with unpredictable consequences.

>         srand(time(NULL));
>         sprintf(randRate ,"%d", rates[rand()%ratescount]);

Now here you don't even pass the required pointer to a
char array but an int as the first argument. That's what
the compiler notices and complains about. This seems to
indicate that you should again think about what sprintf()
actually does, i.e. producing a string from the rest of
the arguments, and that it's your resonsibility to supply
it with the memory (via the first argument) it needs to
put that string into.

>         srand(time(NULL));
>         sprintf(randJob  ,"%s", jobs [rand()%jobscount]);
>         printf("%s, %d, %s\n",randName, randRate, randJob);
>    }

There doesn't seen to be any good reason here for using
sprintf() at all. All you want to do seems to be to out-
put the results, so your whole loop can bxse shortened to

    srand(time(NULL));
    for (i = 0; i < atoi(argv[1]); i++)
        printf("%s, %d, %s\n", names[rand() % namescount],
                               rates[rand() % ratescount],
                               jobs[rand()  % jobscount]);

                      Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 00:39 -0400
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 01:03 -0400
  Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 06:55 +0000
    Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 08:59 -0400
      Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 14:15 +0100
      Re: What am I doing wrong here? Gareth Owen <gwowen@gmail.com> - 2017-05-13 14:19 +0100
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:06 -0400
          Re: What am I doing wrong here? Manfred <noname@invalid.add> - 2017-05-13 17:38 +0200
            Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 13:38 -0400
              Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 19:03 +0100
          Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 16:43 +0100
      Re: What am I doing wrong here? Barry Schwarz <schwarzb@dqel.com> - 2017-05-13 07:44 -0700
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:25 -0400
          Re: What am I doing wrong here? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-13 19:23 +0100
    Re: What am I doing wrong here? Keith Thompson <kst-u@mib.org> - 2017-05-13 13:58 -0700
      Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 23:43 +0000
        Re: What am I doing wrong here? Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 13:43 -0700
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-15 20:18 -0400

csiph-web