What does this program do? 
October 21st, 2006, 04:35 PM
| | | What does this program do?
#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... | 
October 21st, 2006, 04:45 PM
| | | Re: What does this program do?
howa wrote: Quote:
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?
--
Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!! | 
October 21st, 2006, 04:45 PM
| | | Re: What does this program do?
howa wrote: Quote:
#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 ? | 
October 21st, 2006, 05:05 PM
| | | Re: What does this program do?
On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
<howachen@gmail.comwrote, Quote:
>#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'; | 
October 21st, 2006, 05:55 PM
| | | Re: What does this program do?
David Harmon 寫道: Quote:
On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
<howachen@gmail.comwrote, Quote:
#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. | 
October 21st, 2006, 06:15 PM
| | | Re: What does this program do?
howa wrote: Quote:
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.) Quote:
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!
--
Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!! | 
October 21st, 2006, 06:25 PM
| | | Re: What does this program do?
howa wrote: Quote:
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. Quote:
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. Why? What do you think is the purpose of argc if not to tell your program
about the number of command line arguments? | 
October 21st, 2006, 06:25 PM
| | | Re: What does this program do?
On 21 Oct 2006 11:06:57 -0700 in comp.lang.c++, "howa"
<howachen@gmail.comwrote, Quote:
>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.) | 
October 23rd, 2006, 02:55 AM
| | | Re: What does this program do?
On Sat, 21 Oct 2006 11:06:57 -0700, howa wrote: Quote:
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. | 
October 23rd, 2006, 03:45 AM
| | | Re: What does this program do?
noone wrote: Quote:
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.
--
Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!! | 
October 23rd, 2006, 01:45 PM
| | | Re: What does this program do?
In article <pan.2006.10.23.03.12.55.465236@all.com>,
noone <noone@all.comwrote: Quote:
>On Sat, 21 Oct 2006 11:06:57 -0700, howa wrote: Quote:
>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.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? | 
October 23rd, 2006, 08:05 PM
| | | Re: What does this program do?
howa wrote: Quote:
#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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|