[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Determining Number of Arguments argv

Owner

4/7/2011 7:52:00 PM

Can this code determine number of aruments?

sizeof argv / sizeof argv[0]

I'd appreciate any comments


10 Answers

Lew Pitcher

4/7/2011 7:56:00 PM

0

On April 7, 2011 15:51, in comp.lang.c, Owner@Owner-PC.com 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.digitalfr... | Just Linux: http://jus...
---------- Slackware - Because I know what I'm doing. ------


Ian Collins

4/7/2011 8:03:00 PM

0

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

Ian Collins

4/7/2011 8:06:00 PM

0

On 04/ 8/11 07:56 AM, Lew Pitcher wrote:
> On April 7, 2011 15:51, in comp.lang.c, Owner@Owner-PC.com 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

China Blue Veins

4/7/2011 8:09:00 PM

0

In article <pan.2011.04.07.19.51.40.78000@Owner-PC.com>,
Owner <Owner@Owner-PC.com> wrote:

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

If you lost argc somehow, the string array argv points to is terminated by a
NULL pointer.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. I am in the Nile.

Owner

4/7/2011 8:26:00 PM

0

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

Nick

4/7/2011 8:36:00 PM

0

Owner <Owner@Owner-PC.com> 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://ca...
Plan trips, see photos, check facilities | http://canalp...

Ralf Damaschke

4/7/2011 8:48:00 PM

0

Ian Collins <ian-news@hotmail.com> 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

Ian Collins

4/7/2011 8:56:00 PM

0

On 04/ 8/11 08:47 AM, Ralf Damaschke wrote:
> Ian Collins<ian-news@hotmail.com> 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.

Oops, yes, you are right.

--
Ian Collins

Joel C. Salomon

4/7/2011 9:32:00 PM

0

"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

Lew Pitcher

4/7/2011 10:09:00 PM

0

On April 7, 2011 16:26, in comp.lang.c, Owner@Owner-PC.com 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.digitalfr... | Just Linux: http://jus...
---------- Slackware - Because I know what I'm doing. ------