Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #109901
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What am I doing wrong here? |
| Date | 2017-05-13 19:23 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87vap4ehtu.fsf@bsb.me.uk> (permalink) |
| References | <of62cn$4ji$3@dont-email.me> <ennoubFn8ntU1@mid.uni-berlin.de> <of6vp1$m8g$2@dont-email.me> <ac6ehc1jasjlrbev11aq7d9jh1e4kav8q0@4ax.com> <of789u$j0n$3@dont-email.me> |
DFS <nospam@dfs.com> writes:
> On 5/13/2017 10:44 AM, Barry Schwarz wrote:
>> On Sat, 13 May 2017 08:59:40 -0400, DFS <nospam@dfs.com> wrote:
>>> It compiles with 3 warnings:
>>> 26: warning: assignment from incompatible pointer type
>>> 27: warning: assignment from incompatible pointer type
>>> 28: warning: assignment from incompatible pointer type
>>
>> Others have explained why you get these diagnostics. Just because
>> your compiler calls them warnings does not mean they aren't serious.
>>> Why are you executing code that does not compile cleanly? You should
>> never do so unless you *thoroughly* understand what the diagnostics
>> are trying to tell you.
>>
>> Welcome to the world of undefined behavior. The most pernicious form
>> of undefined behavior is to appear to work as you intended.
>>
>> Your system has led you astray in 2/3 of you output. The remaining
>> 1/3 that you recognize to be incorrect is a clue that your
>> understanding of the diagnostics is incomplete.
>
>
> Thanks for the stern lesson, Professor.
>
> Below is my 'final working version', which compiles cleanly and runs
> just fine. See any issues?
I would prefer it not to compile clean since it has a small
compiler-delectable problem. You've forgotten to include stdlib so
rand, srand and atoi are all being called without a prototype and you
are relying on the (now very old) rule that an function undeclared
function f is assume to be declared int f(). That's not even allowed in
modern C (i.e. you must get a diagnostic if you compile as C99 or C11).
> --------------------------------------------------------------------
> #include <stdio.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> int i;
> char randName[2];
> int randRate=0;
> char randJob[11];
>
> 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]);
>
> srand(time(NULL));
> for(i=0;i<atoi(argv[1]);i++)
> {
> sprintf(randName, "%s", names[rand() % namescount]);
> randRate = rates[rand() % ratescount];
> sprintf(randJob , "%s", jobs [rand() % jobscount]);
>
> printf("%s, %d, %s\n",randName, randRate, randJob);
> }
>
> return 0;
> }
> --------------------------------------------------------------------
This has the look of working too hard, though maybe you plan some future
changes that make this work pay off. Is there anything wrong with just
printing the chosen values:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
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]);
if (argc > 1) {
srand(time(NULL));
for(int i = 0; i < atoi(argv[1]); i++)
printf("%s, %d, %s\n",
names[rand() % namescount],
rates[rand() % ratescount],
jobs[rand() % jobscount]);
}
return 0;
}
--
Ben.
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