Newbie: pointerof string array

Discussion in 'C++' started by Zalek Bloom, Aug 17, 2003.

  1. Zalek Bloom

    Zalek Bloom Guest

    Hello,

    Why a pointer of a string array does not prints any address, but a
    string itself? Here is my test:

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    void main()
    {
    char c[] = "the string";
    char * pc = &c[0] ;

    cout << "string c = " << c << endl;
    cout << "pointer of c = " << pc << endl ;
    }

    Here are results:

    string c = the string
    pointer of c = the string

    Why pointer of c - pc - does not prints any address?

    Thanks,

    Zalek
    Zalek Bloom, Aug 17, 2003
    #1
    1. Advertising

  2. Zalek Bloom

    Zalek Bloom Guest

    On Sun, 17 Aug 2003 21:58:51 +0100, "John Harrison"
    <> wrote:

    >
    >"Zalek Bloom" <> wrote in message
    >news:...
    >> Hello,
    >>
    >> Why a pointer of a string array does not prints any address, but a
    >> string itself? Here is my test:
    >>
    >> #include <iostream.h>
    >> #include <string.h>
    >> #include <stdlib.h>
    >>
    >> void main()
    >> {
    >> char c[] = "the string";
    >> char * pc = &c[0] ;
    >>
    >> cout << "string c = " << c << endl;
    >> cout << "pointer of c = " << pc << endl ;
    >> }
    >>
    >> Here are results:
    >>
    >> string c = the string
    >> pointer of c = the string
    >>
    >> Why pointer of c - pc - does not prints any address?
    >>
    >> Thanks,
    >>
    >> Zalek
    >>

    >
    >What you have is a pointer to char, not a pointer to a string arrays.
    >Pointers to char are one way of representing strings in C++. If you really
    >want to see the address cast to void*.
    >
    >cout << "pointer of c = " << (void*)pc << endl ;
    >
    >You can do this with c as well (BTW c is not a string, its a char array).
    >You need to get the definitions of terms like string, array and pointer
    >sorted out.
    >
    >cout << "string c = " << (void*)c << endl;
    >
    >john
    >


    John,
    Thanks for explanation, but I have problem to understand your
    sentence:
    "Pointers to char are one way of representing strings in C++".

    Does this mean that string: char c[] = "string";
    and the pointer to char: char *pc = &c[0]
    both are the same thing? What is a difference between them?

    Thanks,

    Zalek
    Zalek Bloom, Aug 17, 2003
    #2
    1. Advertising

  3. Zalek Bloom

    Rolf Magnus Guest

    Zalek Bloom wrote:

    > Hello,
    >
    > Why a pointer of a string array does not prints any address, but a
    > string itself? Here is my test:
    >
    > #include <iostream.h>


    This is not a standard header.

    > #include <string.h>
    > #include <stdlib.h>


    You're not using anything from those two C headers.

    > void main()


    main() _must_ return int.

    > {
    > char c[] = "the string";
    > char * pc = &c[0] ;
    >
    > cout << "string c = " << c << endl;


    In this place, c is implicitly converted to a const char* that points to
    the first element of your array, and that pointer goes to the specified
    operator<<, which interprets that pointer as one to the first character
    of a C style string and writes that string to the standard output.

    > cout << "pointer of c = " << pc << endl ;


    Here, you use an explicitly converted pointer, but the rest is the same,
    so again, the string is printed.

    > }
    Rolf Magnus, Aug 17, 2003
    #3
  4. "Zalek Bloom" <> wrote in message
    news:...
    > On Sun, 17 Aug 2003 21:58:51 +0100, "John Harrison"
    > <> wrote:
    >
    > >
    > >"Zalek Bloom" <> wrote in message
    > >news:...
    > >> Hello,
    > >>
    > >> Why a pointer of a string array does not prints any address, but a
    > >> string itself? Here is my test:
    > >>
    > >> #include <iostream.h>
    > >> #include <string.h>
    > >> #include <stdlib.h>
    > >>
    > >> void main()
    > >> {
    > >> char c[] = "the string";
    > >> char * pc = &c[0] ;
    > >>
    > >> cout << "string c = " << c << endl;
    > >> cout << "pointer of c = " << pc << endl ;
    > >> }
    > >>
    > >> Here are results:
    > >>
    > >> string c = the string
    > >> pointer of c = the string
    > >>
    > >> Why pointer of c - pc - does not prints any address?
    > >>
    > >> Thanks,
    > >>
    > >> Zalek
    > >>

    > >
    > >What you have is a pointer to char, not a pointer to a string arrays.
    > >Pointers to char are one way of representing strings in C++. If you

    really
    > >want to see the address cast to void*.
    > >
    > >cout << "pointer of c = " << (void*)pc << endl ;
    > >
    > >You can do this with c as well (BTW c is not a string, its a char array).
    > >You need to get the definitions of terms like string, array and pointer
    > >sorted out.
    > >
    > >cout << "string c = " << (void*)c << endl;
    > >
    > >john
    > >

    >
    > John,
    > Thanks for explanation, but I have problem to understand your
    > sentence:
    > "Pointers to char are one way of representing strings in C++".
    >
    > Does this mean that string: char c[] = "string";
    > and the pointer to char: char *pc = &c[0]
    > both are the same thing? What is a difference between them?
    >


    The first is an array (because you wrote []) the second is a pointer
    (because you wrote *).

    On the other hand a string (in the sense you are using) is a sequence of
    chars terminated by the null character. An array can hold the chars to form
    a string, and a pointer can point to the first char in a string. So both a
    char array and a pointer to char can represent a string, but neither
    actually is a string.

    As to the difference between arrays and pointers, that's quite subtle. For
    many purposes they are the same, you can use [] on both pointers and arrays
    for instance

    char a[] = "abc";
    char* p = "abc";

    cout << a[0] << p[1];

    One difference is that you cannot assign to an array

    a = "x"; // illegal
    p = "y"; // ok

    But really you need a good book to explain these things. Any good book will
    have a large section on the differences and similarlities between pointers
    and arrays.

    > Thanks,
    >
    > Zalek


    john
    John Harrison, Aug 17, 2003
    #4
  5. Zalek Bloom

    Greg Comeau Guest

    In article <>,
    Zalek Bloom <> wrote:
    >Hello,
    >
    >Why a pointer of a string array does not prints any address, but a
    >string itself? Here is my test:
    >
    >#include <iostream.h>
    >#include <string.h>
    >#include <stdlib.h>
    >
    >void main()
    >{
    > char c[] = "the string";
    > char * pc = &c[0] ;
    >
    > cout << "string c = " << c << endl;
    > cout << "pointer of c = " << pc << endl ;
    >}
    >
    >Here are results:
    >
    >string c = the string
    >pointer of c = the string
    >
    >Why pointer of c - pc - does not prints any address?


    There is a point where it can be argued that arrays and
    pointers in C are broken. That said, the most common
    use tends to be to want the characters of a string.
    If you want to print raw addresses of things, try something
    like a reinterpret_cast to void *.
    --
    Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
    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, Aug 17, 2003
    #5
  6. Zalek Bloom

    Larry Slater Guest

    In article <bhosj9$ikh$01$-online.com>,
    says...

    > Zalek Bloom wrote:
    >
    > main() _must_ return int.
    >
    > > {
    > > char c[] = "the string";
    > > char * pc = &c[0] ;
    > >
    > > cout << "string c = " << c << endl;

    >
    > In this place, c is implicitly converted to a const char*


    To char * not const char *.
    Larry Slater, Aug 18, 2003
    #6
  7. On Mon, 18 Aug 2003 22:00:08 +0200, Larry Slater wrote:

    > In article <bhosj9$ikh$01$-online.com>,
    > says...
    >
    >> Zalek Bloom wrote:
    >>
    >> > char c[] = "the string";
    >> > cout << "string c = " << c << endl;

    >>
    >> In this place, c is implicitly converted to a const char*

    >
    > To char * not const char *.


    It "decays" to a char*, and then that is converted to a char const*.

    Josh
    Josh Sebastian, Aug 19, 2003
    #7
  8. Zalek Bloom

    Greg Comeau Guest

    In article <>,
    Larry Slater <> wrote:
    >In article <bhosj9$ikh$01$-online.com>,
    >says...
    >> Zalek Bloom wrote:
    >> main() _must_ return int.
    >>
    >> > {
    >> > char c[] = "the string";
    >> > char * pc = &c[0] ;
    >> >
    >> > cout << "string c = " << c << endl;

    >>
    >> In this place, c is implicitly converted to a const char*

    >
    >To char * not const char *.


    Just to avoid confusion... since c is a char[], it will most
    directly collapse to a char *. However, one also notes
    that it will be passed to the appropriate op<<, and hence
    "become" a const char *, hence another "level" of
    implicit conversion. Of course, it is no longer c
    at these points.
    --
    Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
    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, Aug 19, 2003
    #8
    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. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,787
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  2. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    474
    David RF
    Sep 3, 2009
  3. Yvon Thoraval
    Replies:
    5
    Views:
    200
    Jason Creighton
    Sep 17, 2003
  4. Alfonso Caponi

    newbie: argh! array of array

    Alfonso Caponi, Dec 23, 2009, in forum: Ruby
    Replies:
    3
    Views:
    93
    Alfonso Caponi
    Dec 24, 2009
  5. darren_uk
    Replies:
    4
    Views:
    122
    Tad McClellan
    Nov 1, 2003
Loading...

Share This Page