Determining Number of Arguments argv

O

Owner

Can this code determine number of aruments?

sizeof argv / sizeof argv[0]

I'd appreciate any comments
 
L

Lew Pitcher

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.
 
I

Ian Collins

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**.
 
O

Owner

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
 
D

Dr Nick

Owner said:
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.
 
R

Ralf Damaschke

Ian Collins said:
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
 
J

Joel C. Salomon

Dr Nick said:
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
 
L

Lew Pitcher

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 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top