Searching \ for '[OT]:C pointer Question' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/language/index.htm?key=c
Search entire site for: 'C pointer Question'.

Exact match. Not showing close matches.
PICList Thread
'[OT]:C pointer Question'
2003\04\08@011733 by Pang

flavicon
face
Hi all,

Been reading the book,  ' The C Programming Language '
by Brian W. Kernighan and Dennis M. Ritchie

Chapter 5,  Section 5.10 Command Line Arguments

There is a line of code at that section -

while ( --argc > 0 && ( *++argv ) [0] == '-' )
     while ( c= *++argv [0] )
              switch (c)  {.......}

I do not understand the portion c = *++argv[0]. From what i understand, the
'result' of that incrementing and dereferencing will be c = argv[0][1],
which is wrong.

Shouldn't the line be replaced with  c = *++argv[1] ?
FYI,
argv[0] = pointer to array of char "find"
argv[1] = pointer to array of char "-nx"
argv[2] = pointer to array of char "pattern"

The online edition of the K&R are as below -
http://www.trunix.org/programlama/c/CProgrammingLanguage/chapter5.html#s5.10

I have checked the typo report for this second edition but found that this
was not mention, so i guess, it must be my wrong interpretation. Anyway, pls
enlighten.

Thanks.

Pang

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\04\08@012858 by Neil Bradley

flavicon
face
> There is a line of code at that section -
>
> while ( --argc > 0 && ( *++argv ) [0] == '-' )
>       while ( c= *++argv [0] )
>                switch (c)  {.......}
>
> I do not understand the portion c = *++argv[0]. From what i understand, the
> 'result' of that incrementing and dereferencing will be c = argv[0][1],
> which is wrong.

Note the first (*++argv)[0] means that argv gets ++'d, so it moves past
argv[0] to argv[1]'s address (skipping past the launch filename).

Now when you do c = *++argv[0], that will increment the pointer of
argv[1] by 1, skipping past the '-'.

Code like the above is highly unreadable (hence your message), and authors
of such atrocities should be taken out and maimed, shot, and otherwise
tortured. ;-) (yes, I know it's K&R, but they weren't the best coders in
the world).

-->Neil

-------------------------------------------------------------------------------
Neil Bradley            In the land of the blind, the one eyed man is not
Synthcom Systems, Inc.  king - he's a prisoner.
ICQ #29402898

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\04\08@013204 by Brian Smith

picon face
> while ( --argc > 0 && ( *++argv ) [0] == '-' )
>       while ( c= *++argv [0] )
>                switch (c)  {.......}
>
> I do not understand the portion c = *++argv[0]. From what i
understand,
> the
> 'result' of that incrementing and dereferencing will be c =
argv[0][1],
> which is wrong.

That line increments the argv pointer, dereferences it once, then
dereferences it again to compare the byte to '-'.

You could replace that while expression with this:
while (1)
{
    argc--;
    if (argc <= 0)
        break;
    argv++;
    if (argv[0][0] != '-')
        break;

>
> Shouldn't the line be replaced with  c = *++argv[1] ?
> FYI,
> argv[0] = pointer to array of char "find"
> argv[1] = pointer to array of char "-nx"
> argv[2] = pointer to array of char "pattern"

The source appears to be correct.  You should examine
the operator precedence rules.  They are a pain, but
crucial to understanding cryptic code like this.

OK, on with the soap box.  Bear with me?  Please
do not write code like this.  Parentheses are your
friend!  Just because some can understand this code
does not mean that most programmers will. It took me
a bit to understand it, so I know that a fresh
graduate is not going to be able to easily maintain
code written in this style.  Be nice to your
co-workers.  :-)

Brian

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\04\08@015420 by Pang

flavicon
face
Thanks Brian and Neil, I will take note on the advice of good coding. I
guess the only confusion here is whether 'argv' is an array name or a
pointer. The whole structure is in MAIN, so my understanding was that it is
an array name, and as such operation like assignment and increment are
illegal. But base on the example, the 'argv' has to be a pointer to be
correct.

Thanks again.

Pang


{Original Message removed}

2003\04\08@025314 by John Dammeyer

flavicon
face
Hi,

Before you get too carried away with beating up on K&R you must remember
the initial target (PDP-10) for the C language.  At that time compilers
were no where near as sophisticated as they are now and often tight code
like that translated literally to a few lines of assembler.  Code that
was more human readable often generated assembler that was much much
larger.

You see the difference in languages like Pascal.  The compiler creates
the same instruction for a P := P + 5; as a C compiler does for C += 5;
And when you think about it, given that assignments were two character
tokens, the Pascal language should have had P += 5;

But,  in either case, for PIC micro-processors,  it's really important
to study the assembler created by the various C expressions and
statements.  You'll quickly find the techniques that make the tightest
code and that will be the way you program.  And someone will take you to
task for not writing it in a more readable version.  Of course when the
application reaches 8193 words and there are only 8192 available,
efficiency suddenly rules.

Cheers,

John Dammeyer



Wireless CAN with the CANRF module now available.
http://www.autoartisans.com/products
Automation Artisans Inc.
Ph. 1 250 544 4950


> {Original Message removed}

2003\04\08@041809 by john chung

flavicon
face
Pang wrote:

> Thanks Brian and Neil, I will take note on the advice of good coding. I
> guess the only confusion here is whether 'argv' is an array name or a
> pointer. The whole structure is in MAIN, so my understanding was that it is
> an array name, and as such operation like assignment and increment are
> illegal. But base on the example, the 'argv' has to be a pointer to be

okay. This is how i view it. argv is a pointer to an array of pointers. each element in the array of
pointers then point to each argument.

argv  arrayof pointers   the argument in the command-line
[] - > []    ->                [filename]
        []    ->                [option 1]
        []    ->                [option 2]

hope this clear things up.
K&R2 does tell you to avoid this type of expression due to it's unnature brevity.

Regards,
John Chung

>
> correct.
>
> Thanks again.
>
> Pang
>
> {Original Message removed}

2003\04\08@050047 by William Chops Westfield

face picon face
   Before you get too carried away with beating up on K&R you must remember
   the initial target (PDP-10) for the C language.

PDP-11, please.  The PDP-10 was an entirely different beast, and doesn't
match up with C at all well.  (36 bit words, not byte addressable, to start
with.  Lovely to program in assembly, but it was the mid-80s before anyone
did a credible C compiler for it.)

BillW

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\04\08@075102 by Bob Ammerman

picon face
The given code works whether argv is an array name or a pointer.

In C, except in the context of 'sizeof', an array name 'decays' into a
pointer to the first element of the array.

Bob Ammerman
RAm Systems

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

2003\04\08@083128 by Sergio Masci

picon face
----- Original Message -----
From: John Dammeyer <spam_OUTjohndTakeThisOuTspamAUTOARTISANS.COM>
To: <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU>
Sent: Tuesday, April 08, 2003 7:42 AM
Subject: Re: [OT]:C pointer Question


{Quote hidden}

You don't think that since C was originally interpreted (and called B) that
this may have had an influence on special efficiency constructs such as +=
and ++ do you?

I really don't know why these are present in C, I'm just speculating.

Regards
Sergio Masci

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

More... (looser matching)
- Last day of these posts
- In 2003 , 2004 only
- Today
- New search...