Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Determining Number of Arguments argv

Reply
Thread Tools

Determining Number of Arguments argv

 
 
Owner
Guest
Posts: n/a
 
      04-07-2011
Can this code determine number of aruments?

sizeof argv / sizeof argv[0]

I'd appreciate any comments


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      04-07-2011
On April 7, 2011 15:51, in comp.lang.c, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Can this code determine number of aruments?
>
> sizeof argv / sizeof argv[0]
>
> I'd appreciate any comments


Assuming that you use argv in the sense of the 2nd argument to main()
i.e.
char **argv;
or
char *argv[];

then, NO]
sizeof argv / sizeof argv[0]
does not give you the number of arguments in the argv[] array.

It /does/ give you
the number of char * pointers that can fit in a char ** pointer;
a singularly useless tidbit of information.


--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-07-2011
On 04/ 8/11 07:51 AM, Owner wrote:
> Can this code determine number of aruments?
>
> sizeof argv / sizeof argv[0]


No. sizeof(argv) is the size of a char**.

What's wrong with using argc?

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-07-2011
On 04/ 8/11 07:56 AM, Lew Pitcher wrote:
> On April 7, 2011 15:51, in comp.lang.c, (E-Mail Removed) wrote:
>
>> Can this code determine number of aruments?
>>
>> sizeof argv / sizeof argv[0]
>>
>> I'd appreciate any comments

>
> Assuming that you use argv in the sense of the 2nd argument to main()
> i.e.
> char **argv;
> or
> char *argv[];
>
> then, NO]
> sizeof argv / sizeof argv[0]
> does not give you the number of arguments in the argv[] array.
>
> It /does/ give you
> the number of char * pointers that can fit in a char ** pointer;
> a singularly useless tidbit of information.


No, it gives you the size of a char**.

--
Ian Collins
 
Reply With Quote
 
Owner
Guest
Posts: n/a
 
      04-07-2011
On Thu, 07 Apr 2011 15:51:40 -0400, Owner wrote:

> Can this code determine number of aruments?
>
> sizeof argv / sizeof argv[0]
>
> I'd appreciate any comments


I need to build a copy of argv in main, so before that

need to determine how many arguments are there, so I can do

this

char *newargv["number of arguments"];

char *newargv[argc] didn't work. it says it has to be

constant expression

sizeof argv did give me size of a pointer which is 4
 
Reply With Quote
 
Dr Nick
Guest
Posts: n/a
 
      04-07-2011
Owner <(E-Mail Removed)> writes:

> On Thu, 07 Apr 2011 15:51:40 -0400, Owner wrote:
>
>> Can this code determine number of aruments?
>>
>> sizeof argv / sizeof argv[0]
>>
>> I'd appreciate any comments

>
> I need to build a copy of argv in main, so before that
>
> need to determine how many arguments are there, so I can do
>
> this
>
> char *newargv["number of arguments"];
>
> char *newargv[argc] didn't work. it says it has to be
>
> constant expression
>
> sizeof argv did give me size of a pointer which is 4


It does (pre-C99) have to be a constant expression. But, if you think
about it, the number of arguments can't be a constant expression - your
program can be run with any number (up to some system dependent limit
perhaps) of arguments.

So you either need to use a compiler that char *newargv[argc] works on,
or do something with malloc and free, or use a not-quite-standard thing
like alloca to get yourself some space.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
Reply With Quote
 
Ralf Damaschke
Guest
Posts: n/a
 
      04-07-2011
Ian Collins <(E-Mail Removed)> wrote:

> On 04/ 8/11 07:56 AM, Lew Pitcher wrote:
>> Assuming that you use argv in the sense of the 2nd argument to
>> main() i.e.
>> char **argv;
>> or
>> char *argv[];
>>
>> then, NO]
>> sizeof argv / sizeof argv[0]
>> does not give you the number of arguments in the argv[] array.
>>
>> It /does/ give you
>> the number of char * pointers that can fit in a char **
>> pointer;
>> a singularly useless tidbit of information.

>
> No, it gives you the size of a char**.


How that? Just one possible example may be sizeof(char**) = 4
and sizeof(char*) = 4. In my math the result of the division
should be 1.

-- Ralf
 
Reply With Quote
 
Joel C. Salomon
Guest
Posts: n/a
 
      04-07-2011
"Dr Nick" wrote:
> So you either need to use a compiler that char *newargv[argc] works on,
> or do something with malloc and free, or use a not-quite-standard thing
> like alloca to get yourself some space.


Also, it should be char *newargv[argc + 1]; recall that argv[arc] is defined.

--Joel
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      04-07-2011
On April 7, 2011 16:26, in comp.lang.c, (E-Mail Removed) wrote:

> On Thu, 07 Apr 2011 15:51:40 -0400, Owner wrote:
>
>> Can this code determine number of aruments?
>>
>> sizeof argv / sizeof argv[0]
>>
>> I'd appreciate any comments

>
> I need to build a copy of argv in main, so before that
>
> need to determine how many arguments are there, so I can do
>
> this
>
> char *newargv["number of arguments"];
>
> char *newargv[argc] didn't work.


Yah. What you want is either
a VLA (variable-length array), which is available in C99,
or
a malloc()ed bit of memory, which is available in all C standards.



Assuming malloc(), you want something like...

char **newargv;

if ((newargv = malloc((argc + 1) * sizeof(char *)) == NULL)
exit(EXIT_FAILURE); /* or some other exception action */


--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
determining the number of output arguments Darren Dale Python 68 11-21-2004 01:21 PM
Re: determining the number of output arguments Jp Calderone Python 4 11-16-2004 08:47 AM
Determining Number of Arguments Trevor M. Lango C++ 3 02-20-2004 02:27 PM
difference(s) between char** argv and char* argv[] David C Programming 10 09-15-2003 06:58 AM
char **argv vs. char* argv[] Bret C Programming 21 09-03-2003 03:24 AM



Advertisments