473,466 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: The type of argv in K&R2

candide <ca*****@free.invalidwrites:
K&R2 §5.10 tells that the second parameter to the main function,
usually called argv, has type pointer to character strings.

Some quotes from the book :

-------------------- 8< ----------------------------------------
"the second (argv, for argument vector) is a pointer to an array of
character strings"

"Since argv is a pointer to an array of pointers,"

"Since argv is a pointer to the beginning of the array of argument strings"
-------------------- >8 ----------------------------------------

In fact, argv is rather an array of strings, as explained in the
preceding § :
argv can't be an array. It is a function parameter and arrays can't
be passed to functions in C. There is indeed an array "out there" but
all main can ever see is a pointer to it.

Not everyone likes the term "array of strings" -- it can be see as a
bit woolly. What is not in question (I hope) is that, inside main,
argv is not an array: it is a pointer to the first element of an array
whose elements are character pointers. When main is called, a further
guarantee is made: that these pointers (if they are not NULL) point to
strings (i.e. that the character arrays pointed to are
null-terminated).

You will hear people say "argv is an array of strings" and that is fine
at the bus stop (if you frequent that sort of bus stop) but only
because everyone know what it really meant: that the second parameter
passed to main is a pointer to an array of pointers that point to strings.

--
Ben.
Jun 27 '08 #1
2 2398
candide said:
Ben Bacarisse a ecrit :
>argv can't be an array.


In K&R, the argv parameter is declared as :

char *argv[]
so the type of argv is array to pointer to char.
No, it's a pointer to pointer to char. It's just that it points to the
first element in an array of pointers to char.

And the C90 standard agrees :

----------------------- 8< ---------------------------------
5.1.2.2.1 Program startup

(...)the array members argv[0] through argv[argc-1] (...)
That's fine - those pointers /are/ array members.
(...)to by the argv array shall be (...)
That isn't fine - it's sloppy wording. But we've been round this loop
before, and the committee aren't about to change anything.

K&R declares argv to be "a pointer to an array of pointers" and it seems
to me to be incorrect.
Sloppy wording again. But note that, just after your cite, K&R says very
unsloppily that "argv... is a pointer to pointer to char", which is right.
It /also/ says "pointer to the beginning of the array of argument
strings". If we take "beginning of the array" as meaning "first member of
the array", that's actually right too.

But yes, what you've succeeded in noting is that both the Standard and K&R2
are guilty of describing argv in rather woolly terms. It has been noted
before, and no doubt it will be noted again.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
Richard Heathfield <rj*@see.sig.invalidwrites:
candide said:
[...]
(...)to by the argv array shall be (...)

That isn't fine - it's sloppy wording. But we've been round this loop
before, and the committee aren't about to change anything.
[...]

I agree that it's sloppy, but a friendly reading (one that makes it
correct) is possible.

If you interpret the phrase "the argv array" to mean "the array to
whose first element argv points", then the wording is correct. Note
that this does *not* imply that argv is itself an array.

But it's perfectly reasonable to intepret the phrase "the argv array"
to mean "the array named ``argv''", and there is no such array
(there's only a pointer named ``argv''). So yes, the wording is
sloppy.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3

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

Similar topics

5
by: David | last post by:
this phrase has been mentioned many times in this NG and it seems like everyone know what it's. is it a book? a standard? or a guide of some sort? where can i read more about K&R2? thanks!
12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
16
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are...
3
by: Sunny | last post by:
Hi, Well this might look like a dumb question to 99.99% of u but what exactly is K&R2 and where can I get it? Secondly alot of people say that you can learn by reading good books on C. I know...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
2
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
15
by: arnuld | last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the rightmost occurence of t in s, or -1 if there is none. PURPOSE: this program prints the index of the rightmost...
0
by: Daniel Pitts | last post by:
candide wrote: All of those are fundamentally the same type, the only real difference is the semantics that the programmer considers (if any at all). It is common to declare it as "char **argv",...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.