What does this program do?

H

howa

#include<iostream>

using namespace std;

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


cout<<sizeof(argv[1]) / sizeof(argv[1][0]) ;
return 0;
};


to run, e.g.

../a.out helloword, it prints out '4'

what does this program do?

thanks...
 
P

Phlip

howa said:
int main(int argc, char* argv[]) {
cout<<sizeof(argv[1]) / sizeof(argv[1][0]) ;

Is this for a test?

What does your tutorial say about sizeof? (Did you read it? ;-)

What do sizeof(char*) and sizeof(char) return?

What is sizeof(char*) / sizeof(char)?

And what is the difference between [] in a variable declaration and [] in a
function's parameters?
 
A

amparikh

howa said:
#include<iostream>

using namespace std;

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


cout<<sizeof(argv[1]) / sizeof(argv[1][0]) ;
return 0;
};


to run, e.g.

./a.out helloword, it prints out '4'

what does this program do?

thanks...


which C/C++ book are you referring to that dont talk about argc and
argv ?
 
D

David Harmon

On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
#include<iostream>

using namespace std;

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


cout<<sizeof(argv[1]) / sizeof(argv[1][0]) ;
return 0;
};

1. sizeof() is always evaluated at compile time and has nothing to
do with any runtime data. It tells you the size of the type of the
argument. It can never substitute for std::string.size(), or even
strlen().

2. to see more, try some different arguments, e.g.
cout << sizeof(argv[1]) << '\n'
<< sizeof(argv[1][0]) << '\n'
<< sizeof(char*) << '\n'
<< sizeof(char*******) << '\n';
 
H

howa

David Harmon 寫é“:
On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
#include<iostream>

using namespace std;

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


cout<<sizeof(argv[1]) / sizeof(argv[1][0]) ;
return 0;
};

1. sizeof() is always evaluated at compile time and has nothing to
do with any runtime data. It tells you the size of the type of the
argument. It can never substitute for std::string.size(), or even
strlen().

2. to see more, try some different arguments, e.g.
cout << sizeof(argv[1]) << '\n'
<< sizeof(argv[1][0]) << '\n'
<< sizeof(char*) << '\n'
<< sizeof(char*******) << '\n';

well, in order to find the number of item in an array, we can use, e.g.
sizeof(arr) / sizeof(arr[0])

how to do this to find the number of item in argv (just forget about
argc)

?

thanks.
 
P

Phlip

howa said:
well, in order to find the number of item in an array, we can use, e.g.
sizeof(arr) / sizeof(arr[0])

That works for arrays. argv, in your example, is a pointer, which is
different. (Sadly, pointers sometimes borrow [] syntax in confusing ways.
Your tutorial will have the grim details.)
how to do this to find the number of item in argv (just forget about
argc)

Why forget about argc?

If you simply must forget about it, remember that argv, if it were declared,
might look like this:

char *args[] = {
"my/program/name",
"a_command_line_argument",
NULL
};

char **argv = args;

That NULL is always at the end, so you can find it by looping through the
argv array and checking argv[x] == NULL.

Now if this was for a test, I have given you almost enough to cheat. If so,
you are only cheating yourself, and you _still_ need to read your tutorial
some more!
 
R

Rolf Magnus

howa said:
well, in order to find the number of item in an array, we can use, e.g.
sizeof(arr) / sizeof(arr[0])

Yes. Unfortunately, argv[1] is not an array. It's a pointer. So from

sizeof(argv[1]) / sizeof(argv[1][0])

you will get the size of a pointer to char divided by the size of a char.
how to do this to find the number of item in argv

?

What do you want? The number of characters in the first argument (which
seems to be what you tried to find out in the program you posted) or the
number of arguments? For the former, use strlen(). The latter is given you
by argc.
(just forget about argc)

Why? What do you think is the purpose of argc if not to tell your program
about the number of command line arguments?
 
D

David Harmon

On 21 Oct 2006 11:06:57 -0700 in comp.lang.c++, "howa"
well, in order to find the number of item in an array, we can use, e.g.
sizeof(arr) / sizeof(arr[0])

sizeof() is always evaluated at compile time and has nothing to
do with any runtime data. It tells you the size of the static type
of the argument. It can never substitute for std::vector.size(), or
certainly not "argc".

Is there some part of "nothing to do with any runtime data" that I
could write more clearly?

The expression you mention is valid in only one very special
circumstance: when arr is a simple array whose size is known at
compile time (and especially NOT some pointer that may point to
somebody else's distant array.)
 
N

noone

well, in order to find the number of item in an array, we can use, e.g.
sizeof(arr) / sizeof(arr[0])

how to do this to find the number of item in argv (just forget about
argc)

you cant forget about argc. at its highest level argv is an array of
pointers and since there is no guarantee that the array will have a null
pointer entry as its last entry you must have some way to know its length.
that's why argc is provided.
 
P

Phlip

noone said:
you cant forget about argc. at its highest level argv is an array of
pointers and since there is no guarantee that the array will have a null
pointer entry as its last entry you must have some way to know its length.
that's why argc is provided.

I thought the NULL sentinel was in the Standard.
 
M

Martin Steen

howa said:
#include<iostream>

using namespace std;

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


cout<<sizeof(argv[1]) / sizeof(argv[1][0]) ;
return 0;
};


to run, e.g.

./a.out helloword, it prints out '4'

what does this program do?

thanks...

argv[1] is a pointer (to a char). On a 32-Bit-System,
the size of a pointer is 4 Bytes.
So the value of sizeof(argv[1]) is 4.

argv[1][0] is the first character of the string that
argv[1] points to. The size of a character is normally 1.

That's why the result is 4 / 1, which is 4.

-Martin
 
G

Greg Comeau

well, in order to find the number of item in an array, we can use, e.g.
sizeof(arr) / sizeof(arr[0])

how to do this to find the number of item in argv (just forget about
argc)

you cant forget about argc. at its highest level argv is an array of
pointers and since there is no guarantee that the array will have a null
pointer entry as its last entry you must have some way to know its length.
that's why argc is provided.

FWIW, the reference is 3.6.1p2:

"IF ARGC IS NONZERO these arguments shall be supplied in argv[0]
through argv[argc-1] as pointers to the initial characters of
null-terminated multibyte strings (NTMBSs) (_lib.multibyte.strings_)
and argv[0] shall be the pointer to the initial character of a NTMBS
that represents the name used to invoke the program or "".
The value of argc shall be nonnegative. THE VALUE OF ARGV[ARGC] SHALL BE 0.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top