Pointer to Pointer to character

Discussion in 'C++' started by howachen@gmail.com, Oct 7, 2006.

  1. Guest

    a simple program...

    #include <iostream>

    using namespace std;

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

    cout<<argv<<endl;
    cout<<**argv<<endl;
    cout<<argv[0][0]<<endl;
    cout<<&argv[0]<<endl;
    cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]?

    }

    why &argv[0] & &(argv[0][0]) difference?
    , Oct 7, 2006
    #1
    1. Advertising

  2. Howachen posted:

    > #include <iostream>



    You need to include "ostream" if you want to use "endl".


    > using namespace std;
    >
    > int main(int argc, char** argv) {
    >
    > cout<<argv<<endl;



    I don't think ostream::eek:perator<< has an overload which takes a char**.


    > cout<<**argv<<endl;



    This will print a single char.


    > cout<<argv[0][0]<<endl;



    As will this. (Because it's equivalent to:

    *(*(argv + 0) + 0)

    which is equal to:

    **argv


    > cout<<&argv[0]<<endl;



    This is equal to:

    &*(argv+0)

    which is equal to:

    argv+0

    which is equal to:

    argv


    > cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]?



    This is equal to:

    &*(*(argv+0)+0)

    which is equal to:

    *(argv+0)

    Which is equal to:

    *argv

    --

    Frederick Gotham
    Frederick Gotham, Oct 7, 2006
    #2
    1. Advertising

  3. Phlip Guest

    howachen wrote:

    > why &argv[0] & &(argv[0][0]) difference?


    Because argv[0] is type char * and argv[0][0] is type char. So their
    addresses are char** and char* respectively.

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
    Phlip, Oct 7, 2006
    #3
  4. Phlip Guest

    > howachen wrote:

    >> why &argv[0] & &(argv[0][0]) difference?


    > Because argv[0] is type char * and argv[0][0] is type char. So their
    > addresses are char** and char* respectively.


    Also because argv[0] is a pointer, so the address of the pointer itself is
    here, while argv[0][0] is a character, so its address is there.

    assert (argv[0] == &argv[0][0]);

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
    Phlip, Oct 7, 2006
    #4
  5. Guest

    Phlip 寫é“:

    > > howachen wrote:

    >
    > >> why &argv[0] & &(argv[0][0]) difference?

    >
    > > Because argv[0] is type char * and argv[0][0] is type char. So their
    > > addresses are char** and char* respectively.

    >
    > Also because argv[0] is a pointer, so the address of the pointer itself is
    > here, while argv[0][0] is a character, so its address is there.
    >
    > assert (argv[0] == &argv[0][0]);
    >
    > --
    > Phlip
    > http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


    thanks first...

    so &argv[0][0] is the pointer to the first element in the first row

    but how to print out this address as integer?

    i can do this with &argv[0], but how to do it with argv[0][0]?

    thanks.
    , Oct 7, 2006
    #5
  6. Frederick Gotham <> writes:

    > Howachen posted:
    >
    >> #include <iostream>

    >
    > You need to include "ostream" if you want to use "endl".
    >


    I don't think so, as <iostream> includes <ostream> - at least
    on my system. But judging from its name, it should be similar
    on other platforms, too.


    /Renee

    --
    For contacting me: klawitter(at)email(dot)de
    --
    "The major difference between a thing that might go wrong and a thing
    that cannot possibly go wrong is that when a thing that cannot
    possibly go wrong goes wrong it usually turns out to be impossible
    to get at or repair." Douglas Adams, Mostly Harmless
    Renee Klawitter, Oct 7, 2006
    #6
  7. BobR Guest

    Renee Klawitter wrote in message <>...
    >Frederick Gotham <> writes:
    >
    >> Howachen posted:
    >>
    >>> #include <iostream>

    >>
    >> You need to include "ostream" if you want to use "endl".
    >>

    >I don't think so, as <iostream> includes <ostream>


    BUT, it's not written/required in the standards.

    > - at least on my system.


    BUT, not on system xyz. What about the future when your implementation
    removes <ostream> from <iostream>?

    > But judging from its name, it should be similar on other platforms, too.


    Usually is, BUT, you can't be sure!

    >/Renee


    If <iostream> includes <ostream> and you include <ostream> again, it won't
    hurt much because of include guards. So, it's best to be safe and include
    both.

    --
    Bob R
    POVrookie
    BobR, Oct 8, 2006
    #7
  8. Ian Collins Guest

    BobR wrote:
    > Renee Klawitter wrote in message <>...
    >
    >>Frederick Gotham <> writes:
    >>
    >>
    >>>Howachen posted:
    >>>
    >>>
    >>>>#include <iostream>
    >>>
    >>>You need to include "ostream" if you want to use "endl".
    >>>

    >>
    >>I don't think so, as <iostream> includes <ostream>

    >
    >
    > BUT, it's not written/required in the standards.
    >

    Considering std::iostream has std::eek:stream as a base class, it would be
    rather hard to avoid <iostream> including <ostream>.

    --
    Ian Collins.
    Ian Collins, Oct 8, 2006
    #8
  9. red floyd Guest

    wrote:
    > Phlip 寫é“:
    >
    >>> howachen wrote:
    >>>> why &argv[0] & &(argv[0][0]) difference?
    >>> Because argv[0] is type char * and argv[0][0] is type char. So their
    >>> addresses are char** and char* respectively.

    >> Also because argv[0] is a pointer, so the address of the pointer itself is
    >> here, while argv[0][0] is a character, so its address is there.
    >>
    >> assert (argv[0] == &argv[0][0]);
    >>
    >> --
    >> Phlip
    >> http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

    >
    > thanks first...
    >
    > so &argv[0][0] is the pointer to the first element in the first row
    >
    > but how to print out this address as integer?
    >
    > i can do this with &argv[0], but how to do it with argv[0][0]?
    >


    std::cout << static_cast<void *>(&argv[0][0]) << std::endl;

    operator<<(ostream&, const char *) is overloaded so as to do the "right
    thing" for C-style strings.
    red floyd, Oct 8, 2006
    #9
  10. Ron Natalie Guest

    Frederick Gotham wrote:
    > Howachen posted:
    >
    >> #include <iostream>

    >
    >
    > You need to include "ostream" if you want to use "endl".
    >
    >
    >> using namespace std;
    >>
    >> int main(int argc, char** argv) {
    >>
    >> cout<<argv<<endl;

    >
    >
    > I don't think ostream::eek:perator<< has an overload which takes a char**.
    >
    >


    There is one that takes a const void* that this will convert to.
    Ron Natalie, Oct 10, 2006
    #10
  11. Marcus Kwok Guest

    Renee Klawitter <> wrote:
    > Frederick Gotham <> writes:
    >
    >> Howachen posted:
    >>
    >>> #include <iostream>

    >>
    >> You need to include "ostream" if you want to use "endl".

    >
    > I don't think so, as <iostream> includes <ostream> - at least
    > on my system. But judging from its name, it should be similar
    > on other platforms, too.


    Techically, Frederick is right. In fact, I actually ran into this issue
    when trying to compile a program using HP-UX's aCC compiler/std library:
    I had #include'd <iostream> but not <ostream> and the compile failed
    because it didn't know what std::endl was, and also #include'ing
    <ostream> fixed the problem.

    Incidentally, there is a thread on this subject on comp.std.c++:

    http://groups.google.com/group/comp.std.c /browse_frm/thread/8f2a7076d43ef290/

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Oct 13, 2006
    #11
  12. howa Guest

    Marcus Kwok 寫é“:

    > Renee Klawitter <> wrote:
    > > Frederick Gotham <> writes:
    > >
    > >> Howachen posted:
    > >>
    > >>> #include <iostream>
    > >>
    > >> You need to include "ostream" if you want to use "endl".

    > >
    > > I don't think so, as <iostream> includes <ostream> - at least
    > > on my system. But judging from its name, it should be similar
    > > on other platforms, too.

    >
    > Techically, Frederick is right. In fact, I actually ran into this issue
    > when trying to compile a program using HP-UX's aCC compiler/std library:
    > I had #include'd <iostream> but not <ostream> and the compile failed
    > because it didn't know what std::endl was, and also #include'ing
    > <ostream> fixed the problem.
    >
    > Incidentally, there is a thread on this subject on comp.std.c++:
    >
    > http://groups.google.com/group/comp.std.c /browse_frm/thread/8f2a7076d43ef290/
    >
    > --
    > Marcus Kwok
    > Replace 'invalid' with 'net' to reply


    I am more interested in:

    is this a standard way to include the extra <ostream> ?

    or just to fix for some rare compilers?
    howa, Oct 15, 2006
    #12
  13. Bo Persson Guest

    howa wrote:
    > Marcus Kwok ??:
    >
    >> Renee Klawitter <> wrote:
    >>> Frederick Gotham <> writes:
    >>>
    >>>> Howachen posted:
    >>>>
    >>>>> #include <iostream>
    >>>>
    >>>> You need to include "ostream" if you want to use "endl".
    >>>
    >>> I don't think so, as <iostream> includes <ostream> - at least
    >>> on my system. But judging from its name, it should be similar
    >>> on other platforms, too.

    >>
    >> Techically, Frederick is right. In fact, I actually ran into this
    >> issue when trying to compile a program using HP-UX's aCC
    >> compiler/std library: I had #include'd <iostream> but not <ostream>
    >> and the compile failed because it didn't know what std::endl was,
    >> and also #include'ing <ostream> fixed the problem.
    >>
    >> Incidentally, there is a thread on this subject on comp.std.c++:
    >>
    >> http://groups.google.com/group/comp.std.c /browse_frm/thread/8f2a7076d43ef290/
    >>
    >> --
    >> Marcus Kwok
    >> Replace 'invalid' with 'net' to reply

    >
    > I am more interested in:
    >
    > is this a standard way to include the extra <ostream> ?


    It isn't extra. :)

    The standard way is to include all headers declaring the names you
    need.

    If you use std::cout, you include <iostream>.
    If you use std::endl, you include <ostream>.

    >
    > or just to fix for some rare compilers?


    There aren't that many compilers anyway.


    Bo Persson
    Bo Persson, Oct 15, 2006
    #13
    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. Velvet
    Replies:
    9
    Views:
    14,801
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    908
    raavi
    Mar 2, 2006
  3. cgbusch
    Replies:
    6
    Views:
    7,488
    Mike Brown
    Sep 2, 2003
  4. mimmo
    Replies:
    4
    Views:
    27,949
  5. KwikRick
    Replies:
    1
    Views:
    356
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
Loading...

Share This Page