What does this program do?

Discussion in 'C++' started by howa, Oct 21, 2006.

  1. howa

    howa Guest

    #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...
     
    howa, Oct 21, 2006
    #1
    1. Advertising

  2. howa

    Phlip Guest

    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!!!
     
    Phlip, Oct 21, 2006
    #2
    1. Advertising

  3. howa

    Guest

    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 ?
     
    , Oct 21, 2006
    #3
  4. howa

    David Harmon Guest

    On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
    <> 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';
     
    David Harmon, Oct 21, 2006
    #4
  5. howa

    howa Guest

    David Harmon 寫é“:

    > On 21 Oct 2006 09:54:26 -0700 in comp.lang.c++, "howa"
    > <> 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.
     
    howa, Oct 21, 2006
    #5
  6. howa

    Phlip Guest

    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!!!
     
    Phlip, Oct 21, 2006
    #6
  7. howa

    Rolf Magnus Guest

    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?
     
    Rolf Magnus, Oct 21, 2006
    #7
  8. howa

    David Harmon Guest

    On 21 Oct 2006 11:06:57 -0700 in comp.lang.c++, "howa"
    <> 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.)
     
    David Harmon, Oct 21, 2006
    #8
  9. howa

    noone Guest

    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.
     
    noone, Oct 23, 2006
    #9
  10. howa

    Phlip Guest

    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!!!
     
    Phlip, Oct 23, 2006
    #10
  11. howa

    Martin Steen Guest

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


    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
     
    Martin Steen, Oct 23, 2006
    #11
  12. howa

    Greg Comeau Guest

    In article <>,
    noone <> wrote:
    >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.


    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?
     
    Greg Comeau, Oct 23, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pipi
    Replies:
    1
    Views:
    639
    Joseph Millar
    Jul 21, 2003
  2. Rey
    Replies:
    4
    Views:
    11,066
    Roedy Green
    Dec 12, 2003
  3. ola
    Replies:
    3
    Views:
    727
    Marco Schmidt
    Feb 16, 2004
  4. Parvsandhu
    Replies:
    2
    Views:
    428
    Thomas Weidenfeller
    Jul 11, 2005
  5. Phi!
    Replies:
    1
    Views:
    206
Loading...

Share This Page