473,802 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char **argv vs. char* argv[]

I'm curious why char** argv is acceptable in the main() declaration.

In the comp.lang.c FAQ (question 6.18) it says that pointers to
pointers and pointers to an array are not interchangable. However the
declaration:

int main(int argc, char** argv)

is common.

How does that work out? Shouldn't the compiler complain if you're not
doing:

int main(int argc, char* argv[])

Thanks,
Bret
Nov 13 '05
21 18900
Arthur J. O'Dwyer wrote:
Tisdale is a troll. Please just correct his posts quietly
and move away; or just ignore them, as I've learned to do.
(... Check Google Groups.)


I checked Google Groups and searched for

Dwyer troll

in the comp.lang.c newsgroup.
It seems that, whenever you lose and argument,
you accuse your opponent of being a troll. ;-)

Nov 13 '05 #11
E. Robert Tisdale wrote:

<snip>
Now, perhaps you are ready to answer Bret's question,
"Shouldn't the compiler complain if you're not doing:

int main(int argc, char* argv[])?"
The compiler may issue a diagnostic for any reason it likes, but it is not
required to issue a diagnostic for int main(int argc, char **argv).

The answer is that it should.
It is not required so to do. See the Standard. The usual section.
I don't know why it doesn't.
I do.
It appears that you believe that
the reason has something to do with legacy code
which treats T t[] as pointer syntax like T t*
*in a function argument list*.
That is not (precisely) the reason that no diagnostic is required. The
reason no diagnostic is required is that no syntax error or constraint
violation is involved.
Please cite and quote the relevant *rational*
from the ANSI/ISO C standard if you can.


See 5.1.2.2.1 of ISO/IEC 9899:1999.
I don't expect Mr Tisdale to understand this, of course,
but some other people might find it interesting.


Don't be unpleasant, especially when you're wrong.


Nice try, but I'm not wrong, and I wasn't being unpleasant. I was simply
pointing out, in that first clause, that I don't think you are capable of
understanding what I had written. It appears, from your reply, that I was
pretty close to the mark.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #12
j
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message news:<3F******* *******@jpl.nas a.gov>...
Richard Heathfield wrote:

Note that your demo program doesn't meet the requirements of the question,
which nowhere mentions char *s[10]. Still, never mind that. Observe:

> cat foo.c
#include <stdio.h>

void foo(char **r, char *s[], char *t[10]) {
printf("sizeof r = %lu\n", (unsigned long)sizeof r);
printf("sizeof s = %lu\n", (unsigned long)sizeof s);
printf("sizeof t = %lu\n", (unsigned long)sizeof t);
}

int main(int argc, char* argv[]) {
if(argc >= 10) {
foo(argv, argv, argv);
}
else {
char *tmp[10];
foo(argv, argv, tmp);
}
return 0;
}
> ./foo 1 2 3 4 5 6 7 8 9 10
sizeof r = 4
sizeof s = 4
sizeof t = 4

Now for some history.
Here are the first few lines of the source code
from a ***very*** early C compiler.
Regular readers will note the irony of my quoting from it:

/* C compiler

Copyright 1972 Bell Telephone Laboratories, Inc.

*/

ossiz 250;
ospace() {} /* fake */

init(s, t)
char s[]; {
extern lookup, symbuf, namsiz;
char symbuf[], sp[];
int np[], i;

i = namsiz;
sp = symbuf;
while(i--)
if ((*sp++ = *s++)=='\0') --s;
np = lookup();
*np++ = 1;
*np = t;
}

Note the usage of np, which is defined as int np[], and used like this:
*np++ = 1;

Clearly, this is pointer syntax. [] was, in fact, used for pointer notation
early in C's development. Eventually, it was changed
but the usage of [] in function parameter lists lives on.
It /still/ means pointer, in that context.

Brian W Kernighan writes, in K&R2 starting at the foot of p99,
"As formal parameters in a function definition,
char s[]; and char *s; are equivalent; we prefer the latter
because it says more explicitly that the parameter is a pointer."


That's pretty good.
Now, perhaps you are ready to answer Bret's question,
"Shouldn't the compiler complain if you're not doing:

int main(int argc, char* argv[])?"


You can either use ``char *argv[]'' or ``char **argv'' or if not
passing any arguments to main, then ``int main(void)''. In all cases
the compiler won't complain. Although this brings me to wondering
about something. In section 6.11.6 of c99 it says:

"6.11.6 Function declarators
1 The use of function declarators with empty parentheses (not
prototype-format parameter
type declarators) is an obsolescent feature."

So something like:

int foo(); would be considered obsolescent?
Also, would:

int main() /* This falls in 6.11.6? */
{
return 0;
}

The second example of main, it being part of a function definition, is
what is throwing me off and am unsure if it is considered obsolescent
as stated in 6.11.6.


The answer is that it should. I don't know why it doesn't.
It appears that you believe that
the reason has something to do with legacy code
which treats T t[] as pointer syntax like T t*
*in a function argument list*.
Please cite and quote the relevant *rational*
from the ANSI/ISO C standard if you can.

I don't expect Mr Tisdale to understand this, of course,
but some other people might find it interesting.


Don't be unpleasant, especially when you're wrong.

Nov 13 '05 #13
j wrote:
You can either use ``char *argv[]'' or ``char **argv''

or if not passing any arguments to main, then ``int main(void)''.
In all cases the compiler won't complain.
Although this brings me to wondering about something.
In section 6.11.6 of c99 it says:

"6.11.6 Function declarators
1 The use of function declarators with empty parentheses
(not prototype-format parameter type declarators)
is an obsolescent feature."

So something like:

int foo();

would be considered obsolescent?
Also, would:

int main(void) { /* This falls in 6.11.6? */
return 0;
}

The second example of main,
it being part of a function definition, is what is throwing me off
and am unsure if it is considered obsolescent as stated in 6.11.6.


I don't know.
Function main is a special case.
The application isn't allowed to call it.
Your definition above would simply ignore any arguments passed to it.

No doubt, several of our indigenous "spirituali sts"
will cite and quote the ANSI/ISO C "scripture"
as the reason why but I don't think any of them know
the *rationale* behind the dictates of the ANSI/ISO C standard.
Part of the problem is that the reasoning behind decision
taken collectively are seldom documented in great any detail
and are lost as time passes. The result is that
some of what appears in the standard is ritualistic
based upon superstitious belief.

Nov 13 '05 #14
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote:
j wrote:
You can either use ``char *argv[]'' or ``char **argv''

or if not passing any arguments to main, then ``int main(void)''.
In all cases the compiler won't complain.
The compiler doesn't have to complain about *any* incorrect declaration
of main - but the three you listed are all correct.
Although this brings me to wondering about something.
In section 6.11.6 of c99 it says:

"6.11.6 Function declarators
1 The use of function declarators with empty parentheses
(not prototype-format parameter type declarators)
is an obsolescent feature."

So something like:

int foo();

would be considered obsolescent?
Yes.
Also, would:

int main(void) { /* This falls in 6.11.6? */
return 0;
}

The second example of main,
it being part of a function definition, is what is throwing me off
and am unsure if it is considered obsolescent as stated in 6.11.6.

No, that's not obsolescent, because it's using a prototype-format
parameter list. On the other hand, this is obsolescent:

int main()
{
return 0;
}

(It doesn't matter whether it's a definition or a declaration).
I don't know.
Function main is a special case.
The application isn't allowed to call it.
No, there's nothing wrong with the program calling its own main()
function.
Your definition above would simply ignore any arguments passed to it.

No doubt, several of our indigenous "spirituali sts"
will cite and quote the ANSI/ISO C "scripture"
as the reason why but I don't think any of them know
the *rationale* behind the dictates of the ANSI/ISO C standard.
Part of the problem is that the reasoning behind decision
taken collectively are seldom documented in great any detail
and are lost as time passes. The result is that
some of what appears in the standard is ritualistic
based upon superstitious belief.


The reason behind obsoleting non-prototyped function declarations and
definitions is quite clear - because it removes the compiler's ability
to type-check function calls, and it gains nothing.

- Kevin.

Nov 13 '05 #15
E. Robert Tisdale wrote:
j wrote:
In section 6.11.6 of c99 it says:

"6.11.6 Function declarators
1 The use of function declarators with empty parentheses
(not prototype-format parameter type declarators)
is an obsolescent feature."

So something like:

int foo();

would be considered obsolescent?

Right.

Also, would:

int main(void) { /* This falls in 6.11.6? */

No, it doesn't. The keyword "void" fills the gap. In other words, this is a
function prototype - a declaration that explicitly documents the types of
its parameters, with void being used in this case to document that there
are /no/ parameters.

return 0;
}

The second example of main,
it being part of a function definition, is what is throwing me off
and am unsure if it is considered obsolescent as stated in 6.11.6.
I don't know.


I do. int main(void) is not obsolescent.
Function main is a special case.
Only in that it is the entry point to the program and must therefore have a
carefully-defined interface.
The application isn't allowed to call it.


What are you wittering on about now? Of course the application is allowed to
call it.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #16
j

"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message
news:bj******** **@hercules.bti nternet.com...
E. Robert Tisdale wrote:
j wrote:
In section 6.11.6 of c99 it says:

"6.11.6 Function declarators
1 The use of function declarators with empty parentheses
(not prototype-format parameter type declarators)
is an obsolescent feature."

So something like:

int foo();

would be considered obsolescent?

Right.

Also, would:

int main(void) { /* This falls in 6.11.6? */

No, it doesn't. The keyword "void" fills the gap. In other words, this is a function prototype - a declaration that explicitly documents the types of
its parameters, with void being used in this case to document that there
are /no/ parameters.

Hm, in my original question I didn't have a ``void''.. not sure how that
ended up there.
return 0;
}

The second example of main,
it being part of a function definition, is what is throwing me off
and am unsure if it is considered obsolescent as stated in 6.11.6.
I don't know.


I do. int main(void) is not obsolescent.
Function main is a special case.


Only in that it is the entry point to the program and must therefore have

a carefully-defined interface.
The application isn't allowed to call it.
What are you wittering on about now? Of course the application is allowed

to call it.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Nov 13 '05 #17
"j" <ja****@bellsou th.net> wrote in
<qG************ ****@bignews4.b ellsouth.net>:
"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote in message
news:bj******* ***@hercules.bt internet.com...
E. Robert Tisdale wrote:
> j wrote:
>> int main(void) { /* This falls in 6.11.6? */


Hm, in my original question I didn't have a ``void''.. not sure how that
ended up there.

It's not magic. It's E.R.T.!
--
Air is water with holes in it.
Nov 13 '05 #18

On Tue, 2 Sep 2003, j wrote:

"Richard Heathfield" <do******@addre ss.co.uk.invali d> wrote...
E. Robert Tisdale wrote: ^^^^^^^^^^^^^^^ ^^
j wrote:

> In section 6.11.6 of c99 it says:
>
> "6.11.6 Function declarators
> 1 The use of function declarators with empty parentheses
> (not prototype-format parameter type declarators)
> is an obsolescent feature." int main(void) { /* This falls in 6.11.6? */
^^^^
No, it doesn't. [...]


Hm, in my original question I didn't have a ``void''.. not sure how that
ended up there.

This is *exactly* why people should reference the OP's question
directly, and ignore Tisdale's "contributions. " In this case,
Tisdale modified your question to say something completely
different, and then two people separately quoted Tisdale's version
without looking at the real post again. (Don't feed the troll,
blah blah blah...)

BTW, Richard, I like that verb "wittering" !

-Arthur
Nov 13 '05 #19
bl***@yahoo.com (Bret) wrote in message news:<a1******* *************** ****@posting.go ogle.com>...
I'm curious why char** argv is acceptable in the main() declaration.

In the comp.lang.c FAQ (question 6.18) it says that pointers to
pointers and pointers to an array are not interchangable. However the
declaration:

int main(int argc, char** argv)

is common.

How does that work out? Shouldn't the compiler complain if you're not
doing:

int main(int argc, char* argv[])

Thanks,
Bret

First of all, the declarator char *argv[] declares argv to be an array
of pointers to char, not a pointer to an array of char.

char a[10]; /* a is a 10-element array of char */
char *a[10]; /* a is a 10-element array of pointer to char */
char (*a)[10]; /* a is a pointer to a 10-element array of char */
char *(*a)[10]; /* a is a pointer to a 10-element array of pointer to
char */

Secondly, in the context of a formal parameter declaration, char **x
and char *x[] are understood to mean the same thing. You cannot pass
a whole array to a function in C (this includes the main() function);
what actually happens is that a pointer to the first element of the
array is passed to the function. Therefore, char **x more correctly
describes what is happening. The [] notation is allowed to imply to
whomever is reading the program that the argument represents an array
of something, not just a pointer to an individual element. As far as
the code is concerned, however, all that's being passed is a pointer
to a single item (the first element of the array).

FWIW, this means you can't use sizeof to determine the array size in
the called function:

int main (void)
{
int x[10];

/* sizeof x == sizeof int * 10 */
foo (x); /* actually passes &x[0] */
return 0;
}

void foo (int *x)
{
/* sizeof x == sizeof int* */
}
Nov 13 '05 #20

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

Similar topics

7
3543
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same time? I thought of a possible solution. However, I am not quite sure if I understood char vs. char*. Any insight is greatly appreciated. Here is the question: ---------------------- ....
1
2841
by: b83503104 | last post by:
When are they not consistent?
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok etc and their implementation.the way compiler treats them and the scenarios where one
5
5348
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: const char * : "pointer to const-qualified char". char *: "pointer to char". Are these pointed-to types compatibles?
12
10093
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
34
31334
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
9
10532
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
11
2958
by: john | last post by:
Hi, at first the code doesn't seem to work. Any ideas?: #include <iostream> #include <cstdlib> int main() { using namespace std;
9
3621
by: jorba101 | last post by:
On my platform, I see that if I do following: void myFunc( const char *myArg ) { char **argv; argv = &myArg; createTask( ....., argv, .... );
5
7602
by: boddah | last post by:
Hi all, I've been working on this code since morning and seems like I need stronger skills on pointers n arrays. I'm trying to read the content of a config file, which has x lines and then pass it to an array of characters. Not really sure if I did use the strtok function correctly, but here's what I got so far.. #include <stdio.h> #include <stdlib.h>
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10529
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10301
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10280
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10058
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9107
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5620
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3788
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2964
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.