473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problematic code in K&R

Hello C programmers,

in K&R (2nd edition) the authors published the following code for dealing with
command line arguments (p. 117):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

while (--argc 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In addition they give the following explanation of this part of the program:

"argc is decremented and argv is incremented before each optional argument.
[...] Notice that *++argv is a pointer to an argument string, so (*++argv)[0]
is its first character. (An alternate valid form would be **++argv.) Because
[] binds tighter than * and ++, the parentheses are necessary; without them
the expression would be taken as *++(argv[0]). In fact, that is what we have
used in the inner loop, where the task is to walk along a specific argument
string. In the inner loop, the expression *++argv[0] increments the pointer
argv[0]!" (p. 117)

Now it seems that this solution (for "walk[ing] along a specific argument
string" by using the construct "*++argv[0]") raised some criticism:

"The sample program increments argv[0]. Although argv is modifiable, and
argv[0][0] is modifiable (if argv[0] is not a null pointer), argv[0] is not
modifiable. (At least, not always.)" (Peter Seebacher)

"117 (§5.10): In the find example, the program increments argv[0]. This is not
specifically forbidden, but not specifically allowed either." (Source:
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)

Now here's my question: Would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

while (--argc 0 && (*++argv)[0] == '-') {
char c, *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
G.

--

E-mail: info<at>simple-line<Punkt>de
Apr 19 '07 #1
1 1209
On Thu, 19 Apr 2007 13:38:07 +0200, Gregor H. <nomail@invalidwrote:

Slightly changed my code (for better agreement with the original one).
>
in K&R (2nd edition) the authors published the following code for dealing with
command line arguments (p. 117):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

while (--argc 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now here's my question: Would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

while (--argc 0 && (*++argv)[0] == '-') {
char *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

G.

--

E-mail: info<at>simple-line<Punkt>de
Apr 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
6
by: christian9997 | last post by:
Hi I would be very helpful if someone could help me with this code. It works fine in IE but when I display it in Netscape or Firefox and I move the mouse from one menu to the other the gap...
7
by: Andante.in.Blue | last post by:
Hello everyone! I've been working with a problematic legacy database for a while. While I am still fairly new to Access, the more I work with the database, the more problems I've uncovered. ...
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
7
by: Rich Grise | last post by:
OK, I don't know if this is Off-Topic for the group(s), because "QT" isn't "Pure C++", and Slackware is a distro, but those guys are sharp. :-) And I've crossposted to sci.electroncs.design because...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
1
by: bogdanm | last post by:
Hello everyone, I am having a probleme with a issuu code when trying to insert it o my site it doesnt work in IE but in mozzila works fine the code is bellow: <div><object...
10
dj12345
by: dj12345 | last post by:
Hi, (Asp.net + Ajax) I am creating a page which will fetch data from server without postbak of a page.. I have 2 controls on this page TextBox and Lable. I have assigned TextBoxWatermark...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.