Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #109877
| Subject | Re: What am I doing wrong here? |
|---|---|
| Newsgroups | comp.lang.c |
| References | <of62cn$4ji$3@dont-email.me> <ennoubFn8ntU1@mid.uni-berlin.de> <of6vp1$m8g$2@dont-email.me> |
| From | bartc <bc@freeuk.com> |
| Message-ID | <RFDRA.43226$3u2.21524@fx31.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2017-05-13 14:15 +0100 |
On 13/05/2017 13:59, DFS wrote:
> If I did want to use sprintf, just for string handling and getting the
> data into variables, why doesn't the below work?
>
> ------------------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
>
> int i;
> char *randName[2];
> int randRate[8];
> char *randJob[11];
> sprintf(randName, "%s", names[rand() % namescount]);
> sprintf(randRate, "%d", rates[rand() % ratescount]);
> sprintf(randJob , "%s", jobs [rand() % jobscount]);
You're still not calling sprintf properly.
What's the significance of the 2, 8, and 11 in those arrays? sprintf
expects a character array, either char[] or an initialised char*, but
you're passing char*[] and int[]. Some pointer of sorts will be passed,
but the wrong one (hence the warnings), and probably not to what you expect.
Try:
char randName[100];
char randRate[100];
char randJob[100];
The 100 is just some way of ensuring a big enough destination buffer.
> printf("%s, %d, %s\n",randName, randRate, randJob);
And print using all %s.
randRate is now a char buffer, but in yours, you were printing randRate
(which was a int array which becomes an int* in an expression), as a number.
--
bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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