difference b/w char arr[], & char *pointer

Discussion in 'C++' started by rajm2019@gmail.com, Jun 9, 2007.

  1. Guest

    hi all,

    i want to know that what is the actual difference b/w the character
    array
    & character pointer.then how u will get the addrees of a char array[]

    char str[]="be silent like u"
    char *p1="be eloquent r u"
    char *p2;
    p2=str;

    but printing the p2 will give me the str
     
    , Jun 9, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > hi all,
    >
    > i want to know that what is the actual difference b/w the character
    > array
    > & character pointer.then how u will get the addrees of a char array[]
    >
    > char str[]="be silent like u"
    > char *p1="be eloquent r u"
    > char *p2;
    > p2=str;
    >
    > but printing the p2 will give me the str


    Most print routines (std::cout, printf, etc...) treat a char pointer as a
    c-style string and do indeed print the string. One simple work aroiund is
    to cast it to some other pointer type and output that. Such as:

    std::cout << reinterpret_cast<int*>( p2 );
    static_cast may(?) also work.
    You can treat str the same way in output to output the address.
     
    Jim Langston, Jun 9, 2007
    #2
    1. Advertising

  3. peter koch Guest

    On 9 Jun., 08:52, wrote:
    > hi all,
    >
    > i want to know that what is the actual difference b/w the character
    > array
    > & character pointer.then how u will get the addrees of a char array[]
    >
    > char str[]="be silent like u"

    This is an array to 17 chars (remember the leading zero). You can
    change the variable, e.g. str[0] = 'B';.
    > char *p1="be eloquent r u"

    This is a const pointer to an array of char. Do not be fooled about
    the missing const.
    > char *p2;
    > p2=str;
    >
    > but printing the p2 will give me the str

    You can of course print both str and p2 because of the way arrays
    decay to a pointer to the first element.

    /Peter
     
    peter koch, Jun 9, 2007
    #3
  4. Gavin Deane Guest

    On 9 Jun, 08:15, peter koch <> wrote:
    > On 9 Jun., 08:52, wrote:> hi all,
    > > char str[]="be silent like u"

    >
    > This is an array to 17 chars (remember the leading zero).


    trailing, not leading.

    > > char *p1="be eloquent r u"

    >
    > This is a const pointer to an array of char. Do not be fooled about
    > the missing const.


    It's a non-const pointer to const char.

    To the OP: The declaration above is deprecated. You should use

    const char *p1="be eloquent r u";

    for string literals.

    Gavin Deane
     
    Gavin Deane, Jun 9, 2007
    #4
  5. James Kanze Guest

    On Jun 9, 8:52 am, wrote:

    > i want to know that what is the actual difference b/w the
    > character array & character pointer.


    One is an array, and the other is a pointer. What more
    difference do you want?

    > then how u will get the addrees of a char array[]


    By taking its address, using the & operator.

    > char str[]="be silent like u"


    This defines an array, initialized with the characters in the
    string literal.

    > char *p1="be eloquent r u"


    This defines both a pointer and an (unnamed) array---a string
    literal is an unnamed unmodifiable array. The pointer is
    initialized with the address of the first character in the
    unnamed array.

    Note that this is not really correct C++. It's supported for
    historical reasons, but you really should write:

    char const* p1 = "be eloquent r u" ;

    > char *p2;
    > p2=str;


    Here, there is the same implicit conversion as in the previous
    initialization: an array (str) is implicitly converted to the
    address of its first member.

    > but printing the p2 will give me the str


    That's because of a long standing tradition that functions
    (most, anyway) receiving a pointer to a char will treat it as
    the address of the first element of an array of char, and will
    process all of the following elements as well, up to but not
    including a final '\0'.

    In practice, the case almost never comes up in C++, because we
    rarely if ever declare arrays of char, nor use pointer to char.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 9, 2007
    #5
  6. Old Wolf Guest

    On Jun 9, 8:15 pm, Gavin Deane <> wrote:
    > On 9 Jun, 08:15, peter koch <> wrote:
    > > On 9 Jun., 08:52, wrote:> hi all,

    >
    > > > char *p1="be eloquent r u"

    >
    > > This is a const pointer to an array of char.
    > > Do not be fooled about the missing const.

    >
    > It's a non-const pointer to const char.


    To clarify, the type of p1 is 'pointer to char',
    i.e. non-const pointer to non-const char.

    Despite this, the chars it is pointing at are const.
     
    Old Wolf, Jun 10, 2007
    #6
    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. Vinu
    Replies:
    13
    Views:
    1,460
    Lawrence Kirby
    May 12, 2005
  2. yogeshmk

    char *arr[n] Vs. char **arr

    yogeshmk, Jul 17, 2006, in forum: C Programming
    Replies:
    10
    Views:
    499
    deepak
    Jul 18, 2006
  3. Hill Pang

    What's the value of pointer arr?

    Hill Pang, Jul 3, 2011, in forum: C Programming
    Replies:
    9
    Views:
    362
    Tim Rentsch
    Jul 4, 2011
  4. F. Da Costa
    Replies:
    4
    Views:
    110
    Thomas 'PointedEars' Lahn
    Feb 16, 2004
  5. John Reye
    Replies:
    5
    Views:
    310
    Tim Rentsch
    May 31, 2012
Loading...

Share This Page