confused between char and char* and connection to Arrays

Discussion in 'C++' started by arnuld, Mar 29, 2007.

  1. arnuld

    arnuld Guest

    i am trying to understand arrays and char. Stroustrup says, this a
    string literal:

    "this is a string literal"

    he says it is of type /const char/. he also says, because of keeping
    compatibilities with previous definitions of C & C++, it is also a
    CHAR POINTER a.k.a /char*/. however it is an error, which can not be
    caught at untill runtime, to modify a string literal using such
    pointer:


    char* name = "Plato" // name is an array of 5 char
    name[0] = 'R' // error


    tell me whether i am right or wrong

    # 1 - p is an array of 6 CHARS (including '\0') , right ?

    # 2 - p points to the 1st CHAR, 'P' of name a.ka. "Plato".

    # 3 - it is *exactly same as /const char name[] = "Plato"/

    #4 - what about /const char*[] = "Plato"/. what does this represent ?
    arnuld, Mar 29, 2007
    #1
    1. Advertising

  2. * arnuld:
    > i am trying to understand arrays and char. Stroustrup says, this a
    > string literal:
    >
    > "this is a string literal"
    >
    > he says it is of type /const char/.


    I doubt he says that. The element type is 'const char'. The array is
    of type 'char const [n]' where n is the number if characters in the
    string literal + 1, the +1 for the terminating zero byte.


    > he also says, because of keeping
    > compatibilities with previous definitions of C & C++, it is also a
    > CHAR POINTER a.k.a /char*/.


    I doubt he says that. The array is convertible to 'char*'.


    > however it is an error, which can not be
    > caught at untill runtime, to modify a string literal using such
    > pointer:
    >
    >
    > char* name = "Plato" // name is an array of 5 char
    > name[0] = 'R' // error


    Yes.



    > tell me whether i am right or wrong
    >
    > # 1 - p is an array of 6 CHARS (including '\0') , right ?


    Where is the definition of 'p'?

    Perhaps you mean 'name'.

    'name' above is a pointer that points to the first element of an array
    of 6 chars.


    > # 2 - p points to the 1st CHAR, 'P' of name a.ka. "Plato".


    Assuming you mean 'name', yes.


    > # 3 - it is *exactly same as /const char name[] = "Plato"/


    No.


    > #4 - what about /const char*[] = "Plato"/. what does this represent ?


    That should not compile.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 29, 2007
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > On Mar 29, 1:28 pm, "Alf P. Steinbach" <> wrote:

    > * arnuld:


    > > "this is a string literal"

    >
    > > he says it is of type /const char/.

    >
    > I doubt he says that. The element type is 'const char'. The array is
    > of type 'char const [n]' where n is the number if characters in the
    > string literal + 1, the +1 for the terminating zero byte.


    you wording is clear(er) than him :)


    > > he also says, because of keeping
    > > compatibilities with previous definitions of C & C++, it is also a
    > > CHAR POINTER a.k.a /char*/.

    >
    > I doubt he says that. The array is convertible to 'char*'.



    it is not same but it is convertible. so where the difference between /
    char const/ and /char*/ lies ?


    > > tell me whether i am right or wrong

    >
    > > # 1 - p is an array of 6 CHARS (including '\0') , right ?

    >
    > Where is the definition of 'p'?
    >
    > Perhaps you mean 'name'.


    yes, exactly, i apologize for putting confusion.


    > 'name' above is a pointer that points to the first element of an array
    > of 6 chars.


    thanks


    > > # 3 - it is *exactly same as /const char name[] = "Plato"/

    >
    > No.



    i have already asked it: "where the difference lies ?"


    > > #4 - what about /const char*[] = "Plato"/. what does this represent ?

    >
    > That should not compile.


    :-(, i am a poor programmer

    thanks Alf
    arnuld, Mar 29, 2007
    #3
  4. * arnuld:
    >
    >>> # 3 - it is *exactly same as /const char name[] = "Plato"/

    >> No.

    >
    >
    > i have already asked it: "where the difference lies ?"


    char const a[] = "Plato";
    char const* p = "Pluto";

    a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
    where 1 is the size of a char, which has size 1 per definition).

    p is a pointer to the first element of an array of size chars.
    sizeof(p) = the size of a pointer is on your system, probably 4, and it
    depends not on the size of the array at all.

    One thing you can do with the array and not with the pointer is to find
    the number of elements:

    size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

    Try the same with the pointer:

    size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

    (The reason the last expression evaluates to the size of a pointer is
    that p[0] by definition means *(p+0) which by definition means *p which
    is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 29, 2007
    #4
  5. arnuld

    Gavin Deane Guest

    On 29 Mar, 09:38, "arnuld" <> wrote:
    > > On Mar 29, 1:28 pm, "Alf P. Steinbach" <> wrote:
    > > * arnuld:
    > > > # 3 - it is *exactly same as /const char name[] = "Plato"/

    >
    > > No.

    >
    > i have already asked it: "where the difference lies ?"


    char* name1 = "Plato";
    const char name2[] = "Plato";

    Well the fundamental difference is that name1 is of type pointer-to-
    char and name2 is of type array-of-const-char. Arrays and pointers are
    different things.

    Another difference is that

    *name1 = 'X'; will compile but the behaviour is undefined.
    name2[0] = 'X'; will not compile because the chars in the array are
    const.

    This difference is not to do with arrays vs pointers, it is because,
    as far as the compiler is concerned, name1 points to NON-CONST data
    while name2 is an array of CONST data.

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #5
  6. arnuld

    arnuld Guest

    > On Mar 29, 1:50 pm, "Alf P. Steinbach" <> wrote:


    > char const a[] = "Plato";
    > char const* p = "Pluto";
    >
    > a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
    > where 1 is the size of a char, which has size 1 per definition).


    that's clear now.


    > p is a pointer to the first element of an array of size chars.
    > sizeof(p) = the size of a pointer is on your system, probably 4, and it
    > depends not on the size of the array at all.
    >
    > One thing you can do with the array and not with the pointer is to find
    > the number of elements:
    >
    > size_t const aSize = sizeof(a)/sizeof(a[0]); // 6
    >
    > Try the same with the pointer:
    >
    > size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.
    >
    > (The reason the last expression evaluates to the size of a pointer is
    > that p[0] by definition means *(p+0) which by definition means *p which
    > is a char which by definition has size 1, so that sizeof(p[0]) is 1.)


    i got it. "p" is a pointer to the 1st element of an array and "a" is
    an array. hence we are dealing with 2 different "C++ types" exactly
    like "int" and "char" are different.

    BTW, tell me one thing. you used:

    char const* p = "Pluto";

    it is a "pointer" to a "const".

    if you could have used: "char *const p" then "pointer" itself is a
    constant.
    arnuld, Mar 29, 2007
    #6
  7. arnuld

    arnuld Guest

    On Mar 29, 1:57 pm, "Gavin Deane" <> wrote:
    > On 29 Mar, 09:38, "arnuld" <> wrote:
    >
    > > > On Mar 29, 1:28 pm, "Alf P. Steinbach" <> wrote:
    > > > * arnuld:
    > > > > # 3 - it is *exactly same as /const char name[] = "Plato"/

    >
    > > > No.

    >
    > > i have already asked it: "where the difference lies ?"

    >
    > char* name1 = "Plato";
    > const char name2[] = "Plato";
    >
    > Well the fundamental difference is that name1 is of type pointer-to-
    > char and name2 is of type array-of-const-char. Arrays and pointers are
    > different things.
    >
    > Another difference is that
    >
    > *name1 = 'X'; will compile but the behaviour is undefined.
    > name2[0] = 'X'; will not compile because the chars in the array are
    > const.
    >
    > This difference is not to do with arrays vs pointers, it is because,
    > as far as the compiler is concerned, name1 points to NON-CONST data
    > while name2 is an array of CONST data.
    >
    > Gavin Deane


    i got it, we are dealing with 2 DIFFERENT "C++ types"
    arnuld, Mar 29, 2007
    #7
  8. arnuld wrote:
    >> On Mar 29, 1:50 pm, "Alf P. Steinbach" <> wrote:

    >
    >
    >> char const a[] = "Plato";
    >> char const* p = "Pluto";
    >>
    >> a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
    >> where 1 is the size of a char, which has size 1 per definition).

    >
    > that's clear now.
    >
    >
    >> p is a pointer to the first element of an array of size chars.
    >> sizeof(p) = the size of a pointer is on your system, probably 4, and it
    >> depends not on the size of the array at all.
    >>
    >> One thing you can do with the array and not with the pointer is to find
    >> the number of elements:
    >>
    >> size_t const aSize = sizeof(a)/sizeof(a[0]); // 6
    >>
    >> Try the same with the pointer:
    >>
    >> size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.
    >>
    >> (The reason the last expression evaluates to the size of a pointer is
    >> that p[0] by definition means *(p+0) which by definition means *p which
    >> is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

    >
    > i got it. "p" is a pointer to the 1st element of an array and "a" is
    > an array. hence we are dealing with 2 different "C++ types"


    Yes.

    > exactly like "int" and "char" are different.


    I wouldn't say "exactly", since arrays can decay to pointers
    and chars can't decay to ints:

    voif foo(int[] param) {
    cout << sizeof(param); // surprise: the size of a pointer
    }

    Admittedly, this is rather confusing --
    -- C++ pretends you can pass an array to a function,
    while you can actually pass a pointer only. The array
    magically 'decays' to a pointer. So I would say that
    arrays and pointers are more related than ints and chars.

    HTH,
    - J.
    Jacek Dziedzic, Mar 29, 2007
    #8
  9. arnuld

    Daniel T. Guest

    In article <>,
    "arnuld" <> wrote:

    > > On Mar 29, 1:50 pm, "Alf P. Steinbach" <> wrote:

    >
    >
    > > char const a[] = "Plato";
    > > char const* p = "Pluto";
    > >
    > > a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
    > > where 1 is the size of a char, which has size 1 per definition).

    >
    > that's clear now.
    >
    >
    > > p is a pointer to the first element of an array of size chars.
    > > sizeof(p) = the size of a pointer is on your system, probably 4, and it
    > > depends not on the size of the array at all.
    > >
    > > One thing you can do with the array and not with the pointer is to find
    > > the number of elements:
    > >
    > > size_t const aSize = sizeof(a)/sizeof(a[0]); // 6
    > >
    > > Try the same with the pointer:
    > >
    > > size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.
    > >
    > > (The reason the last expression evaluates to the size of a pointer is
    > > that p[0] by definition means *(p+0) which by definition means *p which
    > > is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

    >
    > i got it. "p" is a pointer to the 1st element of an array and "a" is
    > an array. hence we are dealing with 2 different "C++ types" exactly
    > like "int" and "char" are different.
    >
    > BTW, tell me one thing. you used:
    >
    > char const* p = "Pluto";
    >
    > it is a "pointer" to a "const".
    >
    > if you could have used: "char *const p" then "pointer" itself is a
    > constant.


    Right. What matters is which side of the '*' the 'const' is on. Some
    people like to write "const char*" some people like to write "char
    const*" but they are both the same thing and both are very different
    than "char* const".
    Daniel T., Mar 29, 2007
    #9
  10. arnuld

    James Kanze Guest

    On Mar 29, 2:51 pm, Jacek Dziedzic
    <> wrote:
    > arnuld wrote:
    > >> On Mar 29, 1:50 pm, "Alf P. Steinbach" <> wrote:


    > >> char const a[] = "Plato";
    > >> char const* p = "Pluto";


    [...]
    > > i got it. "p" is a pointer to the 1st element of an array and "a" is
    > > an array. hence we are dealing with 2 different "C++ types"


    > Yes.


    > > exactly like "int" and "char" are different.


    > I wouldn't say "exactly", since arrays can decay to pointers
    > and chars can't decay to ints:


    The standard doesn't use the word decay. It says that an array
    can convert implicitly to a pointer. And that a char can
    convert implicitly to an int.

    There is, of course, a difference: when you convert a char to
    int, normally, no information is lost, and you can convert the
    int back to a char, and get the same value. Whereas when you
    convert an array to a pointer, you loose information, and you
    can never go back.

    > voif foo(int[] param) {
    > cout << sizeof(param); // surprise: the size of a pointer
    > }


    > Admittedly, this is rather confusing --
    > -- C++ pretends you can pass an array to a function,
    > while you can actually pass a pointer only.


    Thats a different issue. For reasons of C compatibility, "void
    f(int param[])" is exactly the same type as "void f(int*
    param)".

    Where C compatibility isn't involved, this doesn't occur, and
    you can write something like:
    void f( int (&param)[ 25 ] )
    and can only pass it an array of 25 ints (and not a pointer).
    This is regularly used in things like:

    template< typename T, size_t N >
    T*
    begin( T (&array)[ N ] )
    {
    return array ;
    }

    template< typename T, size_t N >
    T*
    end( T (&array)[ N ] )
    {
    return array + N ;
    }

    in order to get "iterators" into a C style array.

    > The array
    > magically 'decays' to a pointer. So I would say that
    > arrays and pointers are more related than ints and chars.


    On the other hand, ints and chars are both integral types, with
    the same set of operators defined over them, so they're strongly
    related.

    --
    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, Mar 29, 2007
    #10
  11. arnuld

    arnuld Guest

    On Mar 29, 5:51 pm, Jacek Dziedzic
    <> wrote:
    > arnuld wrote:
    > >> On Mar 29, 1:50 pm, "Alf P. Steinbach" <> wrote:

    >
    > >> char const a[] = "Plato";
    > >> char const* p = "Pluto";

    >
    > >> a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
    > >> where 1 is the size of a char, which has size 1 per definition).

    >
    > > that's clear now.

    >
    > >> p is a pointer to the first element of an array of size chars.
    > >> sizeof(p) = the size of a pointer is on your system, probably 4, and it
    > >> depends not on the size of the array at all.

    >
    > >> One thing you can do with the array and not with the pointer is to find
    > >> the number of elements:

    >
    > >> size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

    >
    > >> Try the same with the pointer:

    >
    > >> size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

    >
    > >> (The reason the last expression evaluates to the size of a pointer is
    > >> that p[0] by definition means *(p+0) which by definition means *p which
    > >> is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

    >
    > > i got it. "p" is a pointer to the 1st element of an array and "a" is
    > > an array. hence we are dealing with 2 different "C++ types"

    >
    > Yes.
    >
    > > exactly like "int" and "char" are different.

    >
    > I wouldn't say "exactly", since arrays can decay to pointers
    > and chars can't decay to ints:
    >
    > voif foo(int[] param) {
    > cout << sizeof(param); // surprise: the size of a pointer
    >
    > }
    >
    > Admittedly, this is rather confusing --
    > -- C++ pretends you can pass an array to a function,
    > while you can actually pass a pointer only. The array
    > magically 'decays' to a pointer. So I would say that
    > arrays and pointers are more related than ints and chars.



    hey J, i understood that

    :)
    arnuld, Mar 29, 2007
    #11
  12. arnuld

    arnuld Guest

    On Mar 29, 5:57 pm, "Daniel T." <> wrote:


    > > BTW, tell me one thing. you used:

    >
    > > char const* p = "Pluto";

    >
    > > it is a "pointer" to a "const".

    >
    > > if you could have used: "char *const p" then "pointer" itself is a
    > > constant.

    >
    > Right. What matters is which side of the '*' the 'const' is on. Some
    > people like to write "const char*" some people like to write "char
    > const*" but they are both the same thing and both are very different
    > than "char* const".


    wait....

    char const* p; // pointer to a constant
    char *const p; // constant pointer to a char

    char* const p;

    aaaaahhhooooooo.....

    WHAT IS THAT ?
    arnuld, Mar 29, 2007
    #12
  13. arnuld

    Gavin Deane Guest

    On 29 Mar, 15:48, "arnuld" <> wrote:
    > char *const p; // constant pointer to a char
    >
    > char* const p;
    >
    > aaaaahhhooooooo.....
    >
    > WHAT IS THAT ?


    Calm down, this one's easy :)

    They are the same as

    char * const p;
    char * const p;

    They are all a const pointers to non-cost char. Just with different,
    and equally allowable, use of whitespace. Nothing more.

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #13
  14. arnuld

    arnuld Guest

    > On Mar 29, 8:25 pm, "Gavin Deane" <> wrote:

    > > char *const p; // constant pointer to a char

    >
    > >

    >
    > > aaaaahhhooooooo.....

    >
    > > WHAT IS THAT ?

    >
    > Calm down, this one's easy :)
    >
    > They are the same as
    >
    > char * const p;
    > char * const p;


    > They are all a const pointers to non-cost char. Just with different,
    > and equally allowable, use of whitespace. Nothing more.



    my example was: "char* const p"

    neither "char * const" nor "char *const p"

    BTW, here is the confusion:

    "char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
    "char* const cp" -- const pointer to char [FAQ]


    which one i should believe ?

    same for:

    "char const* pc" -- pointer to const char [STROUSTRUP, 5.4.1]
    "const Fred* p" -- p points to a Fred that is const [FAQ]
    arnuld, Mar 29, 2007
    #14
  15. arnuld

    Gavin Deane Guest

    On 29 Mar, 17:17, "arnuld" <> wrote:
    > > On Mar 29, 8:25 pm, "Gavin Deane" <> wrote:
    > > > char *const p; // constant pointer to a char

    >
    > > > aaaaahhhooooooo.....

    >
    > > > WHAT IS THAT ?

    >
    > > Calm down, this one's easy :)

    >
    > > They are the same as

    >
    > > char * const p;
    > > char * const p;
    > > They are all a const pointers to non-cost char. Just with different,
    > > and equally allowable, use of whitespace. Nothing more.

    >
    > my example was: "char* const p"
    >
    > neither "char * const" nor "char *const p"


    Yep. Those are all the same.

    > BTW, here is the confusion:
    >
    > "char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
    > "char* const cp" -- const pointer to char [FAQ]
    >
    > which one i should believe ?


    Believe both. And while you're at it, believe this too:

    char * const cp -- const pointer to char [ME, JUST NOW]

    How you arrange the * and any whitespace between the char and the
    const is entirely up to you. It has no effect on the meaning.

    > same for:
    >
    > "char const* pc" -- pointer to const char [STROUSTRUP, 5.4.1]


    Yes. pc is a pointer to const char. Another way of saying the same
    thing is that pc points to a char that is const.

    > "const Fred* p" -- p points to a Fred that is const [FAQ]


    And here, p is a pointer to const Fred. Another way of saying the same
    thing is that p points to a Fred that is const.

    I'm not sure what your confusion is in this last example, so I'm not
    sure if my comments help.

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #15
  16. arnuld

    arnuld Guest

    > On Mar 29, 9:26 pm, "Gavin Deane" <> wrote:

    > arnuld wrote:



    > Yep. Those are all the same.


    =:-0

    > > BTW, here is the confusion:


    > > "char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
    > > "char* const cp" -- const pointer to char [FAQ]


    > > which one i should believe ?

    >
    > Believe both. And while you're at it, believe this too:


    > char * const cp -- const pointer to char [ME, JUST NOW]


    ok, i got it now. in all these 3 cases "*" always binds with "const".


    > > "char const* pc" -- pointer to const char [STROUSTRUP, 5.4.1]


    > Yes. pc is a pointer to const char. Another way of saying the same
    > thing is that pc points to a char that is const.
    >
    > > "const Fred* p" -- p points to a Fred that is const [FAQ]

    >
    > And here, p is a pointer to const Fred. Another way of saying the same
    > thing is that p points to a Fred that is const.
    >
    > I'm not sure what your confusion is in this last example, so I'm not
    > sure if my comments help.


    actually, confusion was everywhere and [ME, JUST NOW] helped a lot ;-)

    thanks Gavin, i got it.

    now i will write without looking at any FAQ, Stroustrup and your
    comments:

    char* const p == char *const p == char * const p

    all of them said: "p is a constant pointer to a char"


    char const* p == const char* p

    both said: "p is pointer to const char"


    right ?

    (really i did not look at the book/FAQS/[ME, JUST NOW]

    ;-)
    arnuld, Mar 29, 2007
    #16
  17. arnuld

    Gavin Deane Guest

    On 29 Mar, 17:41, "arnuld" <> wrote:
    > > On Mar 29, 9:26 pm, "Gavin Deane" <> wrote:
    > > arnuld wrote:
    > > Yep. Those are all the same.

    >
    > =:-0
    >
    > > > BTW, here is the confusion:
    > > > "char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
    > > > "char* const cp" -- const pointer to char [FAQ]
    > > > which one i should believe ?

    >
    > > Believe both. And while you're at it, believe this too:
    > > char * const cp -- const pointer to char [ME, JUST NOW]

    >
    > ok, i got it now. in all these 3 cases "*" always binds with "const".


    Yes. The whitespace around the * doesn't affect the meaning.

    It has just occurred to me that maybe nobody has mentioned to you that
    the trick with these is to read them right-to-left. I hope you have a
    fixed pitch font...

    char * const cp;
    ^ ^ ^ ^
    4 3 2 1

    Reading right-to-left: "cp [1] is a const [2] pointer [3] to char
    [4]". The same works for

    char* const cp;
    ^ ^ ^ ^
    4 3 2 1

    and

    char *const cp;
    ^ ^ ^ ^
    4 3 2 1

    as long as you remember that the * is a separate entity in the
    declaration syntax, regardless of whether it has whitespace on either
    side or not.

    <snip>

    > now i will write without looking at any FAQ, Stroustrup and your
    > comments:
    >
    > char* const p == char *const p == char * const p
    >
    > all of them said: "p is a constant pointer to a char"


    Yep.

    > char const* p == const char* p
    >
    > both said: "p is pointer to const char"
    >
    > right ?


    Yep.

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #17
  18. arnuld

    arnuld Guest

    > On Mar 29, 9:59 pm, "Gavin Deane" <> wrote:

    > On 29 Mar, 17:41, "arnuld" <> wrote:


    > Yes. The whitespace around the * doesn't affect the meaning.
    >
    > It has just occurred to me that maybe nobody has mentioned to you that
    > the trick with these is to read them right-to-left. I hope you have a
    > fixed pitch font...
    >
    > char * const cp;
    > ^ ^ ^ ^
    > 4 3 2 1
    >
    > Reading right-to-left: "cp [1] is a const [2] pointer [3] to char
    > [4]". The same works for
    >
    > char* const cp;
    > ^ ^ ^ ^
    > 4 3 2 1
    >
    > and
    >
    > char *const cp;
    > ^ ^ ^ ^
    > 4 3 2 1
    >
    > as long as you remember that the * is a separate entity in the
    > declaration syntax, regardless of whether it has whitespace on either
    > side or not.



    you are the 1st one who explained that "*" is a "separate identity". i
    got it now. your 4-step process gives complete understanding.


    > > char* const p == char *const p == char * const p

    >
    > > all of them said: "p is a constant pointer to a char"

    >
    > Yep.


    :)

    > > char const* p == const char* p

    >
    > > both said: "p is pointer to const char"

    >
    > > right ?

    >
    > Yep.


    :)

    Is Gavin Deane a good guy ?

    yep

    ;-)
    arnuld, Mar 29, 2007
    #18
  19. arnuld

    Default User Guest

    arnuld wrote:

    > > On Mar 29, 1:28 pm, "Alf P. Steinbach" <> wrote:

    >
    > > * arnuld:

    >
    > > > "this is a string literal"

    > >
    > > > he says it is of type /const char/.

    > >
    > > I doubt he says that. The element type is 'const char'. The array
    > > is of type 'char const [n]' where n is the number if characters in
    > > the string literal + 1, the +1 for the terminating zero byte.

    >
    > you wording is clear(er) than him :)


    He says (TC++P3, pg 90):

    quote:
    The type of a string literal is "array of the appropriate number of
    const characters", so "Bohr" is of type const char[5].


    "Bohr" is the example literal on that page. That section very
    thoroughly discusses the string literal. I don't see how you read that
    and didn't understand. It looked very clear to me.



    Brian
    Default User, Mar 29, 2007
    #19
  20. James Kanze wrote:
    > [snip]


    Thanks for clarification!

    - J.
    Jacek Dziedzic, Mar 30, 2007
    #20
    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. wwj
    Replies:
    7
    Views:
    538
  2. Santa
    Replies:
    1
    Views:
    1,061
    Mark A. Odell
    Jul 17, 2003
  3. wwj
    Replies:
    24
    Views:
    2,479
    Mike Wahler
    Nov 7, 2003
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,010
    Ian Collins
    May 9, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,105
    Philipp
    Jan 20, 2009
Loading...

Share This Page