code question

B

Bill Cunningham

kandr2 is about the toughest reading there is on C. I'll try again in a
couple of days when my concentration can be focused.

Bill
 
R

Richard

Bill Cunningham said:
kandr2 is about the toughest reading there is on C. I'll try again in a
couple of days when my concentration can be focused.

Bill

K&R is the best introduction to a programming language I have seen. Bar
none. It does things at the right level without being anal retentive
about stuff the beginner really does not need to know. half a day spent
on chapter 5 "Pointers and Arrays" will have most competent, sentient
beings up and running with pointers. I cant promise understanding the
clc preoccupation with arrays and pointers but even that is probably
good enough too.
 
R

Richard

Richard said:
Bill Cunningham said:
Ian Collins said:
The how about

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc > 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

Bill

To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);

ps use a debugger. And tell me my error from posting too quickly
there. Hint : look at what is in argv and what you are interested in.
 
I

Ian Collins

Richard said:
Bill Cunningham said:
Ian Collins said:
The how about

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc > 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;
[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

To confuse you I assume.

Nope, I just posted before refactoring!
 
R

Richard

Ian Collins said:
Richard said:
Bill Cunningham said:
The how about

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
double data[4] = {0.0};

if ( argc > 5 ) {
puts("Too many arguments");
exit(EXIT_FAILURE);
}

unsigned n = 1;

while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;
[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?

To confuse you I assume.

Nope, I just posted before refactoring!

I apologise that I didn't get the tone right there. I wasn't being
insulting to you - I was more hinting that anything could confuse Bill.
 
I

Ian Collins

Richard said:
for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);

At lest my "non C" way of doing it didn't have the bug you have...

I won't spoil the reader's fun by saying what it is.
 
I

Ian Collins

Richard said:
I apologise that I didn't get the tone right there. I wasn't being
insulting to you - I was more hinting that anything could confuse Bill.

I can't argue with that!
 
B

Ben Bacarisse

Richard said:
Bill Cunningham said:
unsigned n = 1;
while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?
To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);

Great disclaimer! "This is better (but it may be wrong)". Yes it is
(wrong). Here is the flaw with using a debugger. It is easier to
think about this code than to paste this into a main function and
debug it.
 
B

Bill Cunningham

Richard said:
And while I am at it, please stop spamming the application development
groups telling them you are trying to write a printer driver when you
could not even tell them the type of printer you are using.

[Testing out THunderbird]

Go back a read the thread for what it says. We were conversing about my
printer make and model. I don't need to defend what's posted.
 
B

Bill Cunningham

Barry Schwarz said:
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
if ((fp = fopen(argv[4], "a")) == NULL) {

At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you. Things like this for example.
That why clc is needed.

Bill
 
I

Ian Collins

Bill said:
Barry Schwarz said:
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you.

Like common sense?
 
R

Richard

Ben Bacarisse said:
Richard said:
Bill Cunningham said:
unsigned n = 1;
while ( n < argc ) {
data[n-1] = strtod(argv[n], NULL);
++n;

[snip]

What's above is a bit of a different method than I would think of. Is that n
minus 1 in the brackets?
Why set n to 1?
To confuse you I assume. Its not a very C way of doing it IMO. Not
tested for ISO C correctness or whether it even works but this is much
clearer if you (and you must as a c programmer) understand for
loops. You should get the idea.

for(int n=0;n<argc;n++)
data[n] = strtod(argv[n+1], NULL);

Great disclaimer! "This is better (but it may be wrong)". Yes it is
(wrong). Here is the flaw with using a debugger. It is easier to
think about this code than to paste this into a main function and
debug it.

I noticed it when hitting return as the thread will show you. Whoops
indeed. And I never used a debugger so where that came from I'm not
sure. My reference to a debugger before was for Bill to see where he seg
faulted with zero arguments.
 
R

Richard

Bill Cunningham said:
Richard said:
And while I am at it, please stop spamming the application development
groups telling them you are trying to write a printer driver when you
could not even tell them the type of printer you are using.

[Testing out THunderbird]

Go back a read the thread for what it says. We were conversing about
my printer make and model. I don't need to defend what's posted.

I know what was said in the thread. You told the guys you wanted to
write a printer driver for it.
 
R

Richard

Ian Collins said:
Bill said:
if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?
A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you.

Like common sense?

Amazing stuff. Really. If he's a troll (and I really think he is) then
he's damn good.
 
I

Ian Collins

Richard said:
Ian Collins said:
Bill said:
On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
if ((fp = fopen(argv[4], "a")) == NULL) {
At the same time, it should be a file name.

That doesn't strike you as slightly odd?

A filename input is an argument. I thought you treated it as such. There are
many things that kandr2 doesn't teach you.
Like common sense?

Amazing stuff. Really. If he's a troll (and I really think he is) then
he's damn good.

Hell, I'm working on a sunny Saturday afternoon so a bit of light
entertainment is called for!
 
C

CBFalconer

Andrew said:
In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while(*argv)
{
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.
 
C

CBFalconer

Bill said:
Someone pointed out to me Richard that an answer to a question I
raised about bitwise operators was on page 49 or kandr2. I read,
and re-read, and re-read page 49 about 3 times and strained to
keep my focus on reading and the text. The question was why is
the unary operator ~ shown like this:

x~34;
and not x=x~34;

Is the question indeed answered on that page?

Are you sure it is not "x ~= 34;"? Have you checked the published
errata sheet? I greatly doubt that K&R have suppressed the blanks
as you have.
 
I

Ian Collins

CBFalconer said:
Andrew said:
In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while(*argv)
{
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.
Did you bother to read Richard's reply?
 
A

Andrew Poelstra

Andrew said:
In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while(*argv)
{
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Oops! The first line should be
while(argv)
or
while(argv != NULL)

...without the dereference.
 
A

Andrew Poelstra

Andrew Poelstra said:
Andrew Poelstra wrote:

Thanks Richard I know I can always count on you. I wondered about
argc and its uses.

In addition to argc, argv is also NULL terminated (IIRC!), so
you can loop through it without needed argc to find out when
to stop.

while(*argv)
{
process(*argv);
++argv;
}

I'm not sure that is safe. Check whether argv[0] can be a NULL.

Oops! The first line should be
while(argv)
or
while(argv != NULL)

..without the dereference.

No, you were right the first time.

Oh, I see. I guess I need more sleep before working with
pointers to pointers any more. =P
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

code 50
sh?tpile of errors 82
seg fault 76
How to fix this code? 1
code 34
pow type problem 6
URGENT 1
How can I view / open / render / display a pdf file with c code? 0

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top