473,583 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference(s) between char** argv and char* argv[]

what's the differences between:

int main(int argc,char* argv[]){
...
}

and:

int main(int argc,char** argv){
...
}

it seems that in the first example, argv is an array of char* and in
the second example it's a pointer to char*? one of my C book tells me
that the difference is horriblly subtle but doesn't actually discribe
what the difference is. i can use them interchangeabll y right?
Nov 13 '05 #1
10 5106

On Wed, 3 Sep 2003, David wrote:

what's the differences between:

int main(int argc,char* argv[]) int main(int argc,char** argv) it seems that in the first example, argv is an array of char* and in
the second example it's a pointer to char*? one of my C book tells me
that the difference is horriblly subtle but doesn't actually discribe
what the difference is. i can use them interchangeably right?


Yes. They're EXACTLY THE SAME THING. You have already been told this,
several times, modulo Tisdale's muddyings. [Ignore him.]

bar_t foo ( baz_t quux[] );

is exactly equivalent to

bar_t foo ( baz_t *quux );

in all respects.

The difference between p[] and *p comes OUTSIDE function prototypes,
where, for example,

extern int arr[];

declares 'arr' to be of type 'array[unspecified size] of int',
otherwise known as an "incomplete array type." Since the type
of 'arr' is incomplete, you can't, for example, take the 'sizeof'
the array. Contrariwise,

extern int *ptr;

declares 'ptr' to be of type 'pointer to int', and is not incomplete.
That is, you can use 'ptr' in the same way you'd use any pointer,
including, of course, taking the 'sizeof' it.

Just don't worry about these things until you have done the exercises
in the first five chapters of K&R (or equivalent). Then you can
start worrying about becoming a language lawyer. :-)

HTH,
-Arthur
Nov 13 '05 #2
David wrote:
what's the differences between:

int main(int argc,char* argv[]){
...
}

and:

int main(int argc,char** argv){
...
}
No differences.
it seems that in the first example, argv is an array of char*
No, it's a pointer to the first element in an array of strings. Its type is
"pointer to pointer to char", despite appearances.
and in
the second example it's a pointer to char*?
Yes, in the second example, argv is a pointer to the first element in an
array of strings. Its type is "pointer to pointer to char".

Read K&R2 pp99-100.
one of my C book tells me
that the difference is horriblly subtle
The author of that book is either mistaken or lying - probably just
mistaken. Consider that to be one black mark against the book, since he
really should have known this. Which book is it?
but doesn't actually discribe
what the difference is.
There is none.
i can use them interchangeabll y right?


Of course. They have the same type. These are just two different ways of
writing the same thing.
--
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 #3
Arthur J. O'Dwyer wrote:
David wrote:
what's the differences between:

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


it seems that in the first example, argv is an array of char* and in
the second example it's a pointer to char*? one of my C book tells me
that the difference is horribly subtle but doesn't actually describe
what the difference is. I can use them interchangeably right?


Yes. They're EXACTLY THE SAME THING.


Please don't shout.
You have already been told this, several times,
When was David told this?
modulo Tisdale's muddying. [Ignore him.]

bar_t foo(baz_t quux[]);

is exactly equivalent to

bar_t foo(baz_t* quux);

in all respects.

The difference between p[] and *p comes OUTSIDE function prototypes,
You probably meant formal argument lists.
baz_t quux[] and baz_t* quux mean the same thing
in the *definition of function foo as well as in its declaration.
where, for example,

extern int arr[];

declares 'arr' to be of type 'array[unspecified size] of int',
otherwise known as an "incomplete array type." Since the type
of 'arr' is incomplete, you can't, for example, take the 'sizeof'
the array. Contrariwise,

extern int *ptr;

declares 'ptr' to be of type 'pointer to int', and is not incomplete.
That is, you can use 'ptr' in the same way you'd use any pointer,
including, of course, taking the 'sizeof' it.

Just don't worry about these things until you have done the exercises
in the first five chapters of K&R (or equivalent). Then you can
start worrying about becoming a language lawyer. :-)


Please cite and quote the FAQ
that you believe is most relevant to David's question.

Nov 13 '05 #4
E. Robert Tisdale wrote:
Arthur J. O'Dwyer wrote:
<snip>

The difference between p[] and *p comes OUTSIDE function prototypes,


You probably meant formal argument lists.


It amounts to the same thing, in this case, so your objection is pointless.
baz_t quux[] and baz_t* quux mean the same thing
in the *definition of function foo as well as in its declaration.


In this code:

int main(int argc, char **argv)
{
return 0;
}

main() is declared, defined, and prototyped, all at once.

A prototype is merely a function declaration in which the types of all the
parameters are given. A definition is also a declaration.

--
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 #5
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bj******* ***@hercules.bt internet.com>.. .
David wrote:
what's the differences between:

int main(int argc,char* argv[]){
...
}

and:

int main(int argc,char** argv){
...
}


No differences.
it seems that in the first example, argv is an array of char*


No, it's a pointer to the first element in an array of strings. Its type is
"pointer to pointer to char", despite appearances.
and in
the second example it's a pointer to char*?


Yes, in the second example, argv is a pointer to the first element in an
array of strings. Its type is "pointer to pointer to char".

Read K&R2 pp99-100.
one of my C book tells me
that the difference is horriblly subtle


The author of that book is either mistaken or lying - probably just
mistaken. Consider that to be one black mark against the book, since he
really should have known this. Which book is it?
but doesn't actually discribe
what the difference is.


There is none.
i can use them interchangeabll y right?


Of course. They have the same type. These are just two different ways of
writing the same thing.


if there is no differences between the 2 and i can use them
interchangeabll y, why does C support the extra syntax? is this one of
those syntactic sugar thing?
just because a[2] looks better than *(a+2)?
Nov 13 '05 #6
"Arthur J. O'Dwyer" <aj*@andrew.cmu .edu> wrote in message news:<Pi******* *************** *************@u nix44.andrew.cm u.edu>...
On Wed, 3 Sep 2003, David wrote:

what's the differences between:

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

it seems that in the first example, argv is an array of char* and in
the second example it's a pointer to char*? one of my C book tells me
that the difference is horriblly subtle but doesn't actually discribe
what the difference is. i can use them interchangeably right?


Yes. They're EXACTLY THE SAME THING. You have already been told this,
several times, modulo Tisdale's muddyings. [Ignore him.]


[snip]

Just don't worry about these things until you have done the exercises
in the first five chapters of K&R (or equivalent). Then you can
start worrying about becoming a language lawyer. :-)


no i am not trying to be a language lawyer. just trying to understand
the fundamental differences between the 2 so i don't use them in the
wrong way. now i wonder why C support 2 difference syntax when one is
enough...
Nov 13 '05 #7
On 4 Sep 2003 12:54:58 -0700, dz****@hotmail. com (David) wrote:
Of course. They have the same type. These are just two different ways of
writing the same thing.


if there is no differences between the 2 and i can use them
interchangeabl ly, why does C support the extra syntax? is this one of
those syntactic sugar thing?
just because a[2] looks better than *(a+2)?


First, note that they are the same thing only in some contexts. Read
Arthur O'Dwyer's posting carefully.

That said, the [] notation is traditional for arrays, of course, and
it or equivalent notation is used in just about every language that
supports arrays. The pointer notation happens to be the way C
implements arrays, so it's equivalent. The choice of notation can
still be significant since it affects the "flavor " of the code, and
can make it clear what the intention and mind-set of the author is.
For example, If I have a table x of integer values, I find it natural
to think of an entry in that table as x[i]. OTOH, in parsing an array
of characters, I find it more natural to assign a pointer to the
beginning and increment the pointer as I work through the array.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #8
David wrote:

<snip. See subject line. The discussion generalises to the exchangeability
of T *p vs T p[] in formal parameter lists.>

if there is no differences between the 2 and i can use them
interchangeabll y,
....in formal parameter lists, yes. Not in the body of a function.

why does C support the extra syntax?
H[iy]st[oe]rical reasons.
is this one of
those syntactic sugar thing?
It's a h[iy]st[oe]rical accident. [] was originally used in C to mean
"pointer", but it was later munged into its current "array" usage. The
vestigial traces of its pointer heritage remain in function parameter
lists, possibly because to remove those traces would have broken too much
existing code.
just because a[2] looks better than *(a+2)?


Does 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 #9
On Thu, 4 Sep 2003 21:06:56 +0000 (UTC), Richard Heathfield
<do******@addre ss.co.uk.invali d> wrote:
David wrote:

<snip. See subject line. The discussion generalises to the exchangeability
of T *p vs T p[] in formal parameter lists.>

if there is no differences between the 2 and i can use them
interchangeabll y,
...in formal parameter lists, yes. Not in the body of a function.

Right. Or, more precisely, in the declaration of a (formal)
parameter, which contrary to a loose statement elsethread can either
be in a prototype *or* in old-style/K&R1 definition; in both cases the
syntax allows a list, although an actual case may be a singleton.
Not at local scope or file scope (outside any function).
why does C support the extra syntax?


H[iy]st[oe]rical reasons.

If you do that, you must also do r{ea,ai}s[oi]ns. Bleah.
is this one of
those syntactic sugar thing?


It's a h[iy]st[oe]rical accident. [] was originally used in C to mean
"pointer", but it was later munged into its current "array" usage. The
vestigial traces of its pointer heritage remain in function parameter
lists, possibly because to remove those traces would have broken too much
existing code.

Nit: in C's history or development, but not actually C. Per dmr's
2HOPL paper, the transition from actual pointers in BCPL and B to
array decay was one of two changes, the other being complex types
(using declaration-follows-use), that distinguished C from new B.

But yes existing code was the reason to retain the optional old syntax
-- which in hindsight may not have been worth all the angst it has
since caused, but it's way too late now.

- David.Thompson1 at worldnet.att.ne t
Nov 13 '05 #10

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

Similar topics

21
18842
by: Bret | last post by:
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.
1
13230
by: gops | last post by:
Hi, Can anyone send me a specific example of getting the differncce between two dates in terms of no of days using a C library function. I have tried using difftime, but I am not successfull so far. Pls can any one help me out. Regards Gopal
33
5549
by: ankursinha | last post by:
Hi, Is it possible to write a C program that prints "Hello World" on screen without having a single semi-colon in the entire program? The extra constraint here is that u r not allowed to use if,while,switch etc. So far,i figured this could be done by insertint the printf statement in main as shown: int main(int argc=printf("Hello...
3
1231
by: Materialised | last post by:
Hi, I often see 2 different declairations of the main() function, and I am unsure of the difference. They are int main(int argc, char *argv) int main(int argc, char **argv) What is the difference? And are they both complient with the ANSI standard? --
11
3399
by: J Wang | last post by:
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv) { struct stat buf;
5
3952
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with...
18
4039
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
9
2250
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
12
6604
by: kevin.eugene08 | last post by:
Hello all, Forgive the somewhat simple question I am sure this will be, but I am having some problem understanding what: char **argv looks like. For instance, i know through trial and error that if I do this: #include <stdio.h> int main(int argc, char *argv) {
0
8182
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. ...
1
7935
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...
0
8193
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...
0
6579
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...
1
5701
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3818
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2333
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
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
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...

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.