Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) (http://www.velocityreviews.com/forums/t316947-int-main-int-argc-char-argv-vs-int-main-int-argc-char-argv.html)

Hal Styli 01-17-2004 11:43 PM

int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 
Is this a style thing?

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

i.e. *argv[] or **argv

Why choose the latter?



Joe Wright 01-18-2004 02:41 AM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 
Hal Styli wrote:
>
> Is this a style thing?
>
> int main(int argc, char *argv[] ) or int main(int argc, char **argv )
>
> i.e. *argv[] or **argv
>
> Why choose the latter?


No reason. The two are identical in the context of function parameters.
I choose one form or the other depending on how I use argv in the
program.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Richard Heathfield 01-18-2004 04:35 AM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 
Hal Styli wrote:

> Is this a style thing?
>
> int main(int argc, char *argv[] ) or int main(int argc, char **argv )
>
> i.e. *argv[] or **argv


Yes, it's a style thing.

> Why choose the latter?


It's a style thing. In a formal parameter list context, char *argv[] and
char **argv mean precisely the same thing. (In other contexts, they do
not.) Choose the one you think is most meaningful. I prefer **, but some
prefer *[], and with perfectly sound reasoning for their preference, so who
am I to tell them they're wrong?

--
Richard Heathfield : binary@eton.powernet.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

pete 01-18-2004 04:41 AM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 
Richard Heathfield wrote:
>
> Hal Styli wrote:
>
> > Is this a style thing?
> >
> > int main(int argc, char *argv[] ) or int main(int argc, char **argv )
> >
> > i.e. *argv[] or **argv

>
> Yes, it's a style thing.
>
> > Why choose the latter?

>
> It's a style thing. In a formal parameter list context, char *argv[] and
> char **argv mean precisely the same thing. (In other contexts, they do
> not.) Choose the one you think is most meaningful. I prefer **, but some
> prefer *[], and with perfectly sound reasoning for their preference, so who
> am I to tell them they're wrong?


Copying *[] out of the standard, is easier than thinking.

--
pete

August Derleth 01-18-2004 05:24 AM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char**argv )
 
Richard Heathfield wrote:

> Hal Styli wrote:
>
>
>>Is this a style thing?
>>
>>int main(int argc, char *argv[] ) or int main(int argc, char **argv )

[snip]
>>Why choose the latter?

>
>
> It's a style thing. In a formal parameter list context, char *argv[] and
> char **argv mean precisely the same thing. (In other contexts, they do
> not.) Choose the one you think is most meaningful. I prefer **, but some
> prefer *[], and with perfectly sound reasoning for their preference, so who
> am I to tell them they're wrong?
>


I chose *[] because I access elements using array notation instead of
pointer arithmetic, and I want to make it as easy as possible for me to
think of argv as an array.

I do the same thing in my own functions:

void strrev(char str[]);

Would reverse an array of char in place, and in that function I would
treat that array as an array, not as a pointer.

int crc32(char *buf, int len);

Would compute the 32-bit cyclical redundancy check of a buffer
containing character values. Inside the function, I'd treat buf as a
pointer (stepping through it with buf++, for example).

It's purely for the benefit of humans, but that's the point, right? :)

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.



Richard Heathfield 01-18-2004 06:47 AM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 
August Derleth wrote:

> Richard Heathfield wrote:
>
>> It's a style thing. In a formal parameter list context, char *argv[] and
>> char **argv mean precisely the same thing. (In other contexts, they do
>> not.) Choose the one you think is most meaningful. I prefer **, but some
>> prefer *[], and with perfectly sound reasoning for their preference, so
>> who am I to tell them they're wrong?
>>

> I chose *[] because I access elements using array notation instead of
> pointer arithmetic, and I want to make it as easy as possible for me to
> think of argv as an array.
>
> I do the same thing in my own functions:
>
> void strrev(char str[]);
>
> Would reverse an array of char in place, and in that function I would
> treat that array as an array, not as a pointer.
>
> int crc32(char *buf, int len);
>
> Would compute the 32-bit cyclical redundancy check of a buffer
> containing character values. Inside the function, I'd treat buf as a
> pointer (stepping through it with buf++, for example).


Perfectly sound reasoning, IMHO.

> It's purely for the benefit of humans, but that's the point, right? :)


Absolutely. Once you have what you consider to be a rational approach (and
here, you clearly do, even if it's one that I don't happen to use myself),
the important thing is to stick to it. Consistency is the key.

--
Richard Heathfield : binary@eton.powernet.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

Sidney Cadot 01-18-2004 12:54 PM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char**argv )
 
August Derleth wrote:

> [...snip...]


> int crc32(char *buf, int len);
>
> Would compute the 32-bit cyclical redundancy check of a buffer
> containing character values.


Wouldn't it be more appropriate if the return type was unsigned? The
resulting value denotes a polynome with binary coefficients, and there
is nothing to justify a special meaning for the sign bit.

Forthermore, if you use signed ints inside the routine, you are going to
do bitwise operations on signed numbers, which may give headaches
concerning portability.

(By pretty much the same token, I'd make the buf's base type an unsigned
char.)

Best regards, Sidney


John L 01-18-2004 08:06 PM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 

"Richard Heathfield" <dontmail@address.co.uk.invalid> wrote in message news:buda5v$d2q$1@sparta.btinternet.com...
> August Derleth wrote:
>
>
> > It's purely for the benefit of humans, but that's the point, right? :)

>
> Absolutely. Once you have what you consider to be a rational approach (and
> here, you clearly do, even if it's one that I don't happen to use myself),
> the important thing is to stick to it. Consistency is the key.
>


The important thing is to distinguish between pointers
to pre-existing arrays, and pointers which will have
space allocated to them inside the function, and cases
somewhere between the two. But C provides no way of doing
this, and half the time the documentation is no help either. :-(

John.



Peter Nilsson 01-18-2004 10:00 PM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char **argv )
 
Sidney Cadot <sidney@jigsaw.nl> wrote in message news:<budvlg$6v7$1@news.tudelft.nl>...
> August Derleth wrote:
>
> > [...snip...]

>
> > int crc32(char *buf, int len);
> >
> > Would compute the 32-bit cyclical redundancy check of a buffer
> > containing character values.

>
> Wouldn't it be more appropriate if the return type was unsigned? The
> resulting value denotes a polynome with binary coefficients, and there
> is nothing to justify a special meaning for the sign bit.
>
> Forthermore, if you use signed ints inside the routine, you are going to
> do bitwise operations on signed numbers, which may give headaches
> concerning portability.
>
> (By pretty much the same token, I'd make the buf's base type an unsigned
> char.)


I'd shoot for...

unsigned long crc32(const void *, size_t);

--
Peter

August Derleth 01-19-2004 02:36 AM

Re: int main(int argc, char *argv[] ) vs int main(int argc, char**argv )
 
Sidney Cadot wrote:
> August Derleth wrote:
>
>> [...snip...]

>
>
>> int crc32(char *buf, int len);
>>
>> Would compute the 32-bit cyclical redundancy check of a buffer
>> containing character values.

>
>
> Wouldn't it be more appropriate if the return type was unsigned? The
> resulting value denotes a polynome with binary coefficients, and there
> is nothing to justify a special meaning for the sign bit.
>
> Forthermore, if you use signed ints inside the routine, you are going to
> do bitwise operations on signed numbers, which may give headaches
> concerning portability.
>
> (By pretty much the same token, I'd make the buf's base type an unsigned
> char.)


I'm certain you're absolutely correct, and I'm also certain I would take
all that into account were I to actually implement a crc32 function in
C. But the prototype above was a brief throwaway and not intended to be
indicative of my coding skills or lack thereof, merely to illustrate a
minor point of my style.

Hell, maybe I should put a Standard Disclaimer in my signature. It
should only run 200-300 lines, max. ;)

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.




All times are GMT. The time now is 12:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.