473,789 Members | 2,375 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 #1
21 18894
Bret <bl***@yahoo.co m> scribbled the following:
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[])


This is because in function parameter declarations, char **foo and
char *foo[] are the same thing. Not just in main(), but in every other
function too. What's more, int **foo and int *foo[] are also the same.
So are int ***foo and int **foo[].
In fact, in general, if T is a type, then T *foo and T foo[] are the
same thing in function parameter declarations. (This goes ONLY one
level deep - T **foo and T foo[][] are a different matter entirely!)
This is all explained by Chris Torek's THE RULE. If used as a value,
or as a function parameter, an array decays into a pointer into its
first element. Therefore you can't really pass arrays into functions
at all. You can pass pointers to arrays, or structures containing
arrays, but not arrays themselves.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Nov 13 '05 #2
In article <a1************ **************@ posting.google. com>,
Bret <bl***@yahoo.co m> wrote:
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.


char **argv declares argv as a pointer to a pointer to a character
char *argv[] declares argv as am array of pointers to a character

There are no pointers to arrays in either case. So whether or not
pointers to arrays are interchangable with pointers to pointers is
irrelevant to whether "char **argv" and "char *argv[]" are both
allowable as the second parameter to main().

The relevant question you would want to ask is whether arrays and
pointers are interchangable. They aren't in the general case, in the
formal parameter list of a function, they more-or-less are with respect
to the outer-most nesting layer.

-- Brett
Nov 13 '05 #3
Bret wrote:
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 interchangeable .
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[])
It probably should complain.
cat f.c int f(char* s[]) {
char** p = s;
}
gcc -Wall -std=c99 -pedantic -c f.c f.c: In function `f':
f.c:2: warning: unused variable `p'
f.c:3: warning: control reaches end of non-void function

C performs an "implicit conversion"
from an array to a pointer to its first element
and from a pointer to an array.

I can write
cat f.c int f(char* s[]) {
char** p = s;
return f(p);
}
gcc -Wall -std=c99 -pedantic -c f.c


and my compiler will not complain.

Nov 13 '05 #4
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in
<3F**********@j pl.nasa.gov>:
Shouldn't the compiler complain if you're not doing:

int main(int argc, char* argv[])
It probably should complain.


Why?
> cat f.c

int f(char* s[]) {
char** p = s;
}
> gcc -Wall -std=c99 -pedantic -c f.c

f.c: In function `f':
f.c:2: warning: unused variable `p'
f.c:3: warning: control reaches end of non-void function


Your compiler complains about you having declared an automatic variable
without using it and to not return a value from a function that is
declared as returning int. This has no relation to the given problem.
C performs an "implicit conversion"
from an array to a pointer to its first element
Right, when used as a value or formal parameter.
and from a pointer to an array.
Huh?
ITYM one can apply the '[]' operator to a pointer like
one would do with an array, so that:

int i = 21;
int a[42];
int *p = a;

leads to:

p[i] == a[i];

I can write
> cat f.c

int f(char* s[]) {
char** p = s;
return f(p);
}
> gcc -Wall -std=c99 -pedantic -c f.c


and my compiler will not complain.


Yes, because this time you use p *and* you (formally) return a
value while in fact f() won't return anything at all as it will
crash your program when called because of infinite recursion.

Irrwahn

--
If it's not on fire, it's a software problem.
Nov 13 '05 #5
Irrwahn Grausewitz wrote:
E. Robert Tisdale wrote:
Shouldn't the compiler complain if you're not doing:

int main(int argc, char* argv[])
It probably should complain.


Why?
> cat f.c

int f(char* s[]) {
char** p = s;
}
> gcc -Wall -std=c99 -pedantic -c f.c

f.c: In function `f':
f.c:2: warning: unused variable `p'
f.c:3: warning: control reaches end of non-void function


Your compiler complains about you having declared an automatic variable
without using it and to not return a value from a function that is
declared as returning int. This has no relation to the given problem.
C performs an "implicit conversion"

from an array to a pointer to its first element


Right, when used as a value or formal parameter.
and from a pointer to an array.


Huh?
ITYM one can apply the '[]' operator to a pointer like
one would do with an array, so that:

int i = 21;
int a[42];
int *p = a;

leads to:

p[i] == a[i];
I can write
> cat f.c

int f(char* s[]) {
char** p = s;
return f(p);
}
> gcc -Wall -std=c99 -pedantic -c f.c


and my compiler will not complain.


Yes, because this time you use p *and* you (formally) return a
value while in fact f() won't return anything at all as it will
crash your program when called because of infinite recursion.


Now that you've caught up with everyone else, please explain
expand f.c #include <stdio.h>

int f(char* s[10]) {
char** p = s;
fprintf(stdout, "%d = sizeof(s)\n", sizeof(s));
fprintf(stdout, "%d = sizeof(p)\n", sizeof(p));
return 0;
}

int main(int argc, char* argv[]) {
char * s[10];
fprintf(stdout, "%d = sizeof(s)\n", sizeof(s));
return f(s);
}
gcc -Wall -std=c99 -pedantic -o main f.c
./main

40 = sizeof(s)
4 = sizeof(s)
4 = sizeof(p)

Nov 13 '05 #6

On Mon, 1 Sep 2003, Irrwahn Grausewitz wrote:

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in
<3F************ **@jpl.nasa.gov >:
Now that you've caught up with everyone else, please explain
> gcc -Wall -std=c99 -pedantic -o main f.c
> ./main

40 = sizeof(s)
4 = sizeof(s)
4 = sizeof(p)


All I can see is that on your system
10*sizeof(char* ) == 40 and sizeof(char**) == 4.
Not very surprisingly, I have to add.

Where's the joke?

Tisdale is a troll. Please just correct his posts quietly and
move away; or just ignore them, as I've learned to do. (And
yes, the "is he really at NASA?" question has been beaten to
death several times already. Check Google Groups.)

-Arthur
Nov 13 '05 #7
E. Robert Tisdale wrote:
Now that you've caught up with everyone else,
Don't be unpleasant, especially when you're wrong.
please explain
Delighted.
> expand f.c


Syntax error.
#include <stdio.h>

int f(char* s[10]) {
char** p = s;
fprintf(stdout, "%d = sizeof(s)\n", sizeof(s));
fprintf(stdout, "%d = sizeof(p)\n", sizeof(p));


At the very least, on systems where a size_t is larger than an int the
behaviour is undefined.

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

rjh@tux:~/scratch> 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;
}

rjh@tux:~/scratch> ./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."

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

--
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 #8
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[])?"

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 #9
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote:
Please cite and quote the relevant *rational*
from the ANSI/ISO C standard if you can.

Read JTC1/SC22/WG14 N843 6.7.5.3#6 if you can,
then rip out the page, eat it & choke.

--
Yah, I know, I should not feed the trolls.
Nov 13 '05 #10

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

Similar topics

7
3542
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
2838
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
5347
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
10091
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
31333
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
9
10530
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
3618
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
9657
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
9502
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10400
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
10189
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
10132
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
6754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4084
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 we have to send another system
2
3688
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.