Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What does this program do?

Reply
Thread Tools

What does this program do?

 
 
howa
Guest
Posts: n/a
 
      10-21-2006
#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...

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      10-21-2006
howa wrote:

> 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!!!


 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      10-21-2006

howa wrote:
> #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 ?

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      10-21-2006
On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
<(E-Mail Removed)> wrote,
>#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';

 
Reply With Quote
 
howa
Guest
Posts: n/a
 
      10-21-2006

David Harmon 寫道:

> On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
> <(E-Mail Removed)> wrote,
> >#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.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      10-21-2006
howa wrote:

> 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!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-21-2006
howa wrote:

> 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?

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      10-21-2006
On 21 Oct 2006 11:06:57 -0700 in comp.lang.c++, "howa"
<(E-Mail Removed)> wrote,
>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.)

 
Reply With Quote
 
noone
Guest
Posts: n/a
 
      10-23-2006
On Sat, 21 Oct 2006 11:06:57 -0700, howa wrote:

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


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      10-23-2006
noone wrote:

> 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!!!


 
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
System program/ Application program ?? Parvsandhu Java 2 07-11-2005 09:08 AM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
how to convert a java program to an exe program ola Java 3 02-16-2004 09:42 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
passing data between Java program and C program--help pipi Java 1 07-21-2003 05:02 AM



Advertisments