473,387 Members | 1,303 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,387 software developers and data experts.

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 2390
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",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
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...

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.