difference between between these "char"s

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

  1. arnuld

    arnuld Guest

    what is the difference between these 2:

    char name = "hackers";
    char* name = "hackers";
     
    arnuld, Mar 4, 2007
    #1
    1. Advertising

  2. arnuld

    arnuld Guest

    > char name = "hackers";

    i do not know anything about this.

    > char* name = "hackers";


    i guess, this will become an array of "char"s and name will point to
    1st element of the array .i.e.

    name and name[0] will mean same

    right ?
     
    arnuld, Mar 4, 2007
    #2
    1. Advertising

  3. arnuld

    Sarath Guest

    On Mar 4, 2:38 pm, "arnuld" <> wrote:
    > > char name = "hackers";

    >
    > i do not know anything about this.
    >
    > > char* name = "hackers";

    >
    > i guess, this will become an array of "char"s and name will point to
    > 1st element of the array .i.e.
    >
    > name and name[0] will mean same
    >
    > right ?


    I dont this think that
    char name = "hackers"; statement is a valid one.

    char name means a variable of type char which has one byte in size.

    Regards,
    Sarath
     
    Sarath, Mar 4, 2007
    #3
  4. arnuld

    Jerry Coffin Guest

    In article <>,
    says...
    > what is the difference between these 2:
    >
    > char name = "hackers";
    > char* name = "hackers";


    The main difference is that the second will compile, but the first one
    won't.

    Perhaps you intended:

    char name[] = "hackers";
    vs.
    char *name = "hackers";

    ?

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Mar 4, 2007
    #4
  5. arnuld

    arnuld Guest

    > On Mar 4, 10:54 am, Jerry Coffin <> wrote:

    > > char name = "hackers";
    > > char* name = "hackers";

    >
    > The main difference is that the second will compile, but the first one
    > won't.


    that i did not know. may i know why ?

    > Perhaps you intended:
    >
    > char name[] = "hackers";
    > vs.
    > char *name = "hackers";
    >
    > ?


    what's the difference between these 2 ?
    > --
    > Later,
    > Jerry.
    >
    > The universe is a figment of its own imagination.
     
    arnuld, Mar 4, 2007
    #5
  6. arnuld

    Ian Collins Guest

    arnuld wrote:
    >>On Mar 4, 10:54 am, Jerry Coffin <> wrote:

    >
    >>>char name = "hackers";
    >>>char* name = "hackers";

    >>
    >>The main difference is that the second will compile, but the first one
    >>won't.

    >
    > that i did not know. may i know why ?


    A char can only be initialised with a character constant, not a string
    literal. A string literal is a const char*.
    >
    >>Perhaps you intended:
    >>
    >>char name[] = "hackers";
    >>vs.
    >>char *name = "hackers";
    >>
    >>?

    >
    > what's the difference between these 2 ?
    >

    The former initialises an array of char with
    {'h','a','c','k','e','r','s','\0'} the latter initialises a char* with
    the address of the string literal "hackers" in this case, the compiler
    may issue a diagnostic due the the type of a string literal being const
    char*

    --
    Ian Collins.
     
    Ian Collins, Mar 4, 2007
    #6
  7. arnuld

    arnuld Guest

    > On Mar 4, 11:45 am, Ian Collins <> wrote:

    > >>char name[] = "hackers";
    > >>vs.
    > >>char *name = "hackers";

    >
    > >>?

    >
    > > what's the difference between these 2 ?

    >
    > The former initialises an array of char with
    > {'h','a','c','k','e','r','s','\0'} the latter initialises a char* with
    > the address of the string literal "hackers" in this case, the compiler
    > may issue a diagnostic due the the type of a string literal being const
    > char*


    it means we can there are 2 ways to create a string:

    an array and
    a char*

    in both cases we will get a string, so it doe snot matter we use which
    one.

    right ?
     
    arnuld, Mar 4, 2007
    #7
  8. arnuld

    Ian Collins Guest

    arnuld wrote:
    >
    > it means we can there are 2 ways to create a string:
    >
    > an array and
    > a char*
    >
    > in both cases we will get a string, so it doe snot matter we use which
    > one.
    >
    > right ?
    >

    Very very wrong!

    char name[] = "hackers";

    Is an automatic array of char, as such, it can be modified.

    char *name = "hackers";

    Is the incorrect way of writing

    const char *name = "hackers";

    A string literal should not be modified, to do so invokes undefined
    behaviour and my cause your toilet to explode.

    --
    Ian Collins.
     
    Ian Collins, Mar 4, 2007
    #8
  9. arnuld

    arnuld Guest

    > On Mar 4, 12:12 pm, Ian Collins <> wrote:

    > arnuld wrote:
    > > in both cases we will get a string, so it doe snot matter we use which
    > > one.

    >
    > > right ?



    > Very very wrong!
    >
    > char name[] = "hackers";
    >
    > Is an automatic array of char, as such, it can be modified.


    ok, i new that.

    > char *name = "hackers";
    >
    > Is the incorrect way of writing
    >
    > const char *name = "hackers";


    that is really strange for me as i did not know. i collect 2 points
    here:

    1.) we can use /char*/ where we are damn sure that we need a constant
    string.

    2.) a string is different from string literal. a /string/ is an /
    array/ of /char/ and a /string literal/ is /char*/ and that can not be
    modified.


    > A string literal should not be modified, to do so invokes undefined
    > behaviour and my cause your toilet to explode.


    whwn will my kitchen explode ?

    ;-)




    > --
    > Ian Collins.
     
    arnuld, Mar 4, 2007
    #9
  10. arnuld

    Ian Collins Guest

    arnuld wrote:
    >
    > 1.) we can use /char*/ where we are damn sure that we need a constant
    > string.
    >

    const char*.

    --
    Ian Collins.
     
    Ian Collins, Mar 4, 2007
    #10
  11. arnuld

    Rolf Magnus Guest

    arnuld wrote:

    >> char *name = "hackers";
    >>
    >> Is the incorrect way of writing
    >>
    >> const char *name = "hackers";

    >
    > that is really strange for me as i did not know. i collect 2 points
    > here:
    >
    > 1.) we can use /char*/ where we are damn sure that we need a constant
    > string.


    Yes, however, this is only true if you directly initialize it with a string
    literal. That's a stupid special rule to make C++ more compatible with C
    code written by sloppy programmers.

    > 2.) a string is different from string literal. a /string/ is an /
    > array/ of /char/ and a /string literal/ is /char*/ and that can not be
    > modified.


    A string literal is also an array of char.
     
    Rolf Magnus, Mar 4, 2007
    #11
  12. arnuld

    arnuld Guest

    > On Mar 4, 3:00 pm, Rolf Magnus <> wrote:

    > > 1.) we can use /char*/ where we are damn sure that we need a constant
    > > string.

    >
    > Yes, however, this is only true if you directly initialize it with a string
    > literal. That's a stupid special rule to make C++ more compatible with C
    > code written by sloppy programmers.


    :-(

    i really hate that decision of Stroustrup.

    > > 2.) a string is different from string literal. a /string/ is an /
    > > array/ of /char/ and a /string literal/ is /char*/ and that can not be
    > > modified.

    >
    > A string literal is also an array of char.


    the difference is a "string literal", is a /const/ array of char

    ?
     
    arnuld, Mar 4, 2007
    #12
  13. arnuld

    Rolf Magnus Guest

    arnuld wrote:

    >> > 2.) a string is different from string literal. a /string/ is an /
    >> > array/ of /char/ and a /string literal/ is /char*/ and that can not be
    >> > modified.

    >>
    >> A string literal is also an array of char.

    >
    > the difference is a "string literal", is a /const/ array of char
    >
    > ?


    I'd call it an array of const char, but yes. Also, no matter where it's
    defined in the source code, it has static storage duration, i.e. exists
    throughout the whole execution time of the program.
     
    Rolf Magnus, Mar 4, 2007
    #13
  14. arnuld

    arnuld Guest

    > On Mar 4, 3:15 pm, Rolf Magnus <> wrote:

    > arnuld wrote:


    > > the difference is a "string literal", is a /const/ array of char

    >
    > > ?

    >
    > I'd call it an array of const char, but yes. Also, no matter where it's
    > defined in the source code, it has static storage duration, i.e. exists
    > throughout the whole execution time of the program.


    thanks, that was new thing you told, static allocation of string
    literal.

    now i can say i know about char*

    :)
     
    arnuld, Mar 4, 2007
    #14
  15. arnuld

    Gavin Deane Guest

    On 4 Mar, 10:36, "arnuld" <> wrote:
    > now i can say i know about char*


    Don't forget that C++ gives you std::string to remove the need to
    worry about these complications. That's not to say you shouldn't
    understand raw char arrays too - you certainly should - but now you've
    learnt how the hard way works don't forget you have the easy way
    available to you too.

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

    Bo Persson Guest

    arnuld wrote:
    >> On Mar 4, 11:45 am, Ian Collins <> wrote:

    >
    >>>> char name[] = "hackers";
    >>>> vs.
    >>>> char *name = "hackers";

    >>
    >>>> ?

    >>
    >>> what's the difference between these 2 ?

    >>
    >> The former initialises an array of char with
    >> {'h','a','c','k','e','r','s','\0'} the latter initialises a char*
    >> with the address of the string literal "hackers" in this case, the
    >> compiler may issue a diagnostic due the the type of a string literal
    >> being const char*

    >
    > it means we can there are 2 ways to create a string:
    >
    > an array and
    > a char*
    >
    > in both cases we will get a string, so it doe snot matter we use which
    > one.
    >
    > right ?


    No, not quite right.

    Have you read chapter 5 yet? :)


    Bo Persson
     
    Bo Persson, Mar 4, 2007
    #16
  17. arnuld

    Bo Persson Guest

    arnuld wrote:
    >> On Mar 4, 3:00 pm, Rolf Magnus <> wrote:

    >
    >>> 1.) we can use /char*/ where we are damn sure that we need a
    >>> constant string.

    >>
    >> Yes, however, this is only true if you directly initialize it with a
    >> string literal. That's a stupid special rule to make C++ more
    >> compatible with C code written by sloppy programmers.

    >
    > :-(
    >
    > i really hate that decision of Stroustrup.


    It wasn't for him to decide, it is inherited from the C language.

    At the time when C++ was designed, C didn't have the keyword const, so all
    string literals were of the type char*. That made code like

    char* x = "Hello";

    valid. In C++ string literals have the type 'const char*', but this old C
    code has got a special dispensation so that it will continue to work anyway.
    Everywhere else it is an error to assign a pointer to const to a pointer to
    non-const.

    Also, even though x is of type char*, in C++ it is still illegal to assign
    to *x. The string is a constant!

    >
    >>> 2.) a string is different from string literal. a /string/ is an /
    >>> array/ of /char/ and a /string literal/ is /char*/ and that can not
    >>> be modified.

    >>
    >> A string literal is also an array of char.

    >
    > the difference is a "string literal", is a /const/ array of char
    >
    > ?


    At this time it could be a good idea to take a peak at chapter 20 about
    std::string, which solves these kinds of problems the C++ way. In C++ you
    hardly ever have to use char* directly.

    You have now found exactly the reason why many in this group recommended
    "Accelerated C++", which introduces std::string on the first page of chapter
    1. The subtle difficulties of pointers and arrays are postponed to chapter
    10, long after teaching about the simpler things like classes, templates and
    overloaded functions.


    Bo Persson
     
    Bo Persson, Mar 4, 2007
    #17
  18. arnuld

    arnuld Guest

    > On Mar 4, 4:51 pm, "Bo Persson" <> wrote:
    > arnuld wrote:


    > > i really hate that decision of Stroustrup.

    >
    > It wasn't for him to decide, it is inherited from the C language.


    that was what i said. his decision for "keepiing backward
    compatibility with C" was a bad decision. we got a complex beast in
    the end, named C++.

    but please leave it here. i do not want it to become the topic of
    discussion. i want the topic to be "chat and char*", so i will not
    talk about any OT things. my mistake.


    > At the time when C++ was designed, C didn't have the keyword const, so all
    > string literals were of the type char*. That made code like
    >
    > char* x = "Hello";
    >
    > valid. In C++ string literals have the type 'const char*', but this old C
    > code has got a special dispensation so that it will continue to work anyway.
    > Everywhere else it is an error to assign a pointer to const to a pointer to
    > non-const.


    what does last sentence mean:

    "a pointer to const to a pointer to non-const"


    this pointer to a char notation:

    char* x; or
    char *x;

    how will you define your last sentence in this notation?

    > Also, even though x is of type char*, in C++ it is still illegal to assign
    > to *x. The string is a constant!


    did not get it. IMVHO, please explain using an example.



    > >>> 2.) a string is different from string literal. a /string/ is an /
    > >>> array/ of /char/ and a /string literal/ is /char*/ and that can not
    > >>> be modified.

    >
    > >> A string literal is also an array of char.

    >
    > > the difference is a "string literal", is a /const/ array of char

    >
    > > ?

    >
    > At this time it could be a good idea to take a peak at chapter 20 about
    > std::string, which solves these kinds of problems the C++ way.


    i know about "std::strings" and understand how they work. i only want
    to understand the "char and char*" for now.


    > In C++ you hardly ever have to use char* directly.


    YES, you use "std::string". i am saying, it is important to understand
    "char and char*" when you have understood "std::string".


    > You have now found exactly the reason why many in this group recommended
    > "Accelerated C++", which introduces std::string on the first page of chapter
    > 1. The subtle difficulties of pointers and arrays are postponed to chapter
    > 10, long after teaching about the simpler things like classes, templates and
    > overloaded functions.


    to me, i do not think pointers are difficult than Templates. i do not
    have any teacher/mentor who can teach me, except you folks. all of the
    colleges here teach a language named VC++ & they do not agree that
    "Linux is a good OS" and i am a UNIX man, that is the trouble.



    > Bo Persson
     
    arnuld, Mar 4, 2007
    #18
  19. arnuld

    arnuld Guest

    > On Mar 4, 12:12 pm, Ian Collins <> wrote:

    > arnuld wrote:
    >


    > Very very wrong!
    >
    > char name[] = "hackers";
    >
    > Is an automatic array of char, as such, it can be modified.
    >
    > char *name = "hackers";
    >
    > Is the incorrect way of writing
    >
    > const char *name = "hackers";
    >
    > A string literal should not be modified, to do so invokes undefined
    > behaviour and my cause your toilet to explode.



    a "const char*" can easily be modified, see:


    #include <iostream>

    int main() {
    int x = 7;
    const char* author = "Stroustrup";

    std::cout << x
    << "\t"
    << author
    <<"\n";

    x = 100;
    author = "Bjarne";

    std::cout << x
    << "\t"
    << author
    <<"\n";
    }

    ---------- OUTPUT
    [arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra char-po.cpp
    [arch@voodo cpp]$ ./a.out
    7 Stroustrup
    100 Bjarne
    [arch@voodo cpp]$
    ------------------------

    no undefined behaviour, i modified the "const char*". now, If i can
    modify "const", what is the significance of "const" then ? [1]


    -- arnuld
    http://arnuld.blogspot.com

    [1] but a "const int/char/float" can not be modified. i have checked
    that.
     
    arnuld, Mar 4, 2007
    #19
  20. arnuld

    Greg Comeau Guest

    In article <>,
    arnuld <> wrote:
    >> On Mar 4, 12:12 pm, Ian Collins <> wrote:

    >
    >> arnuld wrote:
    >>

    >
    >> Very very wrong!
    >>
    >> char name[] = "hackers";
    >>
    >> Is an automatic array of char, as such, it can be modified.
    >>
    >> char *name = "hackers";
    >>
    >> Is the incorrect way of writing
    >>
    >> const char *name = "hackers";
    >>
    >> A string literal should not be modified, to do so invokes undefined
    >> behaviour and my cause your toilet to explode.

    >
    >a "const char*" can easily be modified, see:


    Ian said a string literal should not be modified, he did not say...

    >#include <iostream>
    >
    >int main() {
    > int x = 7;
    > const char* author = "Stroustrup";
    >
    > std::cout << x
    > << "\t"
    > << author
    > <<"\n";
    >
    > x = 100;
    > author = "Bjarne";


    ... that you cannot reassign to a named const char *.
    author is not a string literal in your example, it is a pointer
    to one (actually two of them over your apps lifetime) in your case.

    > std::cout << x
    > << "\t"
    > << author
    > <<"\n";
    >}
    >
    >---------- OUTPUT
    >[arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra char-po.cpp
    >[arch@voodo cpp]$ ./a.out
    >7 Stroustrup
    >100 Bjarne
    >[arch@voodo cpp]$
    >------------------------
    >
    >no undefined behaviour, i modified the "const char*". now, If i can
    >modify "const", what is the significance of "const" then ? [1]


    You didn''t modify const. The const here, and where UB would come
    in, is if you did something like:

    *author = 'b';

    to make his name lowercase.

    >[1] but a "const int/char/float" can not be modified. i have checked
    >that.


    Nor can const other things. But here, the pointer is not const,
    the thing is points to is, or at least should be considered so
    through author.

    And atop that, a string literal, as Ian correctly points out,
    should not be considered a modifiable entity.
    --
    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, Mar 4, 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:
    558
  2. Santa
    Replies:
    1
    Views:
    1,094
    Mark A. Odell
    Jul 17, 2003
  3. wwj
    Replies:
    24
    Views:
    2,521
    Mike Wahler
    Nov 7, 2003
  4. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,063
    Ian Collins
    May 9, 2006
Loading...

Share This Page