what's the difference between the next 2 declarations?

Discussion in 'C Programming' started by Wade Yin, Apr 5, 2004.

  1. Wade Yin

    Wade Yin Guest

    Hi,

    What's the difference between:

    char *x="abcde";

    and

    char y[]={"abcde"};


    I can't modify the value of the variable x in functions, but y can be do.

    Is there any other similar problem need to be notice?



    Thanks in advance!

    Wade Yin
     
    Wade Yin, Apr 5, 2004
    #1
    1. Advertising

  2. Wade Yin

    Jack Klein Guest

    On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <>
    wrote in comp.lang.c:

    > Hi,
    >
    > What's the difference between:
    >
    > char *x="abcde";


    x is a pointer to the a six character (including the '\0') string
    literal. Attempting to modify a string literal causes undefined
    behavior. x itself can be modified to point to a different array of
    characters.

    > and
    >
    > char y[]={"abcde"};


    y is an array of 6 characters, initialized with the string literal.
    The characters in y can be changed.

    > I can't modify the value of the variable x in functions, but y can be do.
    >
    > Is there any other similar problem need to be notice?


    No, the situation with string literals is a special case and there are
    no others like it, except for wide string literals.

    > Thanks in advance!
    >
    > Wade Yin


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Apr 5, 2004
    #2
    1. Advertising

  3. Wade Yin

    Wade Yin Guest

    "Jack Klein" <>
    ??????:...
    > On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <>
    > wrote in comp.lang.c:
    >
    > > Hi,
    > >
    > > What's the difference between:
    > >
    > > char *x="abcde";

    >
    > x is a pointer to the a six character (including the '\0') string
    > literal. Attempting to modify a string literal causes undefined
    > behavior. x itself can be modified to point to a different array of
    > characters.


    How does "abcde" stored in memory? we can find x in memory, but we can't
    operate it?


    >
    > > and
    > >
    > > char y[]={"abcde"};

    >
    > y is an array of 6 characters, initialized with the string literal.
    > The characters in y can be changed.
    >
    > > I can't modify the value of the variable x in functions, but y can be

    do.
    > >
    > > Is there any other similar problem need to be notice?

    >
    > No, the situation with string literals is a special case and there are
    > no others like it, except for wide string literals.
    >
    > > Thanks in advance!
    > >
    > > Wade Yin

    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Wade Yin, Apr 5, 2004
    #3
  4. Wade Yin wrote:
    >>> char *x="abcde";

    >>
    >>x is a pointer to the a six character (including the '\0') string
    >>literal. Attempting to modify a string literal causes undefined
    >>behavior. x itself can be modified to point to a different array of
    >>characters.

    >
    >
    > How does "abcde" stored in memory? we can find x in memory, but we can't
    > operate it?


    Can't "change it", yes, the standard says that you cannot change a
    string literal. The implementation has the freedom to place it in a
    write-only protected memory segment, in ROM, or in fact anywhere it
    desires. It may even put it exactly where it puts a modifiable character
    array.

    --
    John Tsiombikas (Nuclear / the Lab)

    http://thelab.demoscene.gr/nuclear/
     
    John Tsiombikas (Nuclear / the Lab), Apr 5, 2004
    #4
  5. Wade Yin <> spoke thus:

    > How does "abcde" stored in memory?


    Exactly how is (I believe) up to the implementation, but commonly it
    ends up in read-only memory.

    > we can find x in memory, but we can't operate it?


    Not without bad things potentially happening - depending on the
    implementation, attempting to access memory denoted as read-only can
    cause your program to crash.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 5, 2004
    #5
  6. Wade Yin

    Leor Zolman Guest

    On Mon, 5 Apr 2004 13:36:27 +0800, "Wade Yin" <> wrote:

    >
    >"Jack Klein" <>
    >??????:...
    >> On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <>
    >> wrote in comp.lang.c:
    >>
    >> > Hi,
    >> >
    >> > What's the difference between:
    >> >
    >> > char *x="abcde";

    >>
    >> x is a pointer to the a six character (including the '\0') string
    >> literal. Attempting to modify a string literal causes undefined
    >> behavior. x itself can be modified to point to a different array of
    >> characters.

    >
    > How does "abcde" stored in memory? we can find x in memory, but we can't
    >operate it?
    >

    If you go to this page:
    http://www.bdsoft.com/courses/cprog-details.html
    and click on the "slides" link, slides #33-42 illustrate the different ways
    in which your two constructs are stored in memory, and the sorts of things
    you're permitted to do with them.

    (Note to rest of group: I haven't updated this material since I began
    hanging out in clc, so there are probably lots of nits to pick WRT saying
    what's const and what's not, etc. Rest assured I'll go over the entire
    /course/ before I ever actually teach it again....)
    -leor

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 5, 2004
    #6
  7. Wade Yin

    Dan Pop Guest

    In <c4qpfg$7cm$99.com> "Wade Yin" <> writes:

    > What's the difference between:
    >
    > char *x="abcde";
    >
    > and
    >
    > char y[]={"abcde"};


    How about reading the FAQ *before* posting?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 5, 2004
    #7
  8. Wade Yin

    Dan Pop Guest

    In <c4rlhk$8gh$> Christopher Benson-Manica <> writes:

    >Wade Yin <> spoke thus:
    >
    >> How does "abcde" stored in memory?

    >
    >Exactly how is (I believe) up to the implementation, but commonly it
    >ends up in read-only memory.


    It's less common than you might think. Read-only string literals are a
    C89 invention and many implementors have chosen compatibility with
    pre-ANSI C programs. In pre-ANSI C, string literals were writable and
    the compiler was not free to merge multiple identical string literals.
    Some compilers (e.g. gcc) even let the user choose between the two
    flavours of string literals:

    -fwritable-strings
    Store string constants in the writable data segment and don't
    uniquize them. This is for compatibility with old programs
    which assume they can write into string constants.

    >> we can find x in memory, but we can't operate it?

    >
    >Not without bad things potentially happening - depending on the
    >implementation, attempting to access memory denoted as read-only can
    >cause your program to crash.


    Well I can't find anything wrong with read-only accesses to read-only
    memory...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 5, 2004
    #8
  9. Wade Yin

    Jack Klein Guest

    On 5 Apr 2004 15:31:17 GMT, (Dan Pop) wrote in
    comp.lang.c:

    > In <c4rlhk$8gh$> Christopher Benson-Manica <> writes:
    >
    > >Wade Yin <> spoke thus:
    > >
    > >> How does "abcde" stored in memory?

    > >
    > >Exactly how is (I believe) up to the implementation, but commonly it
    > >ends up in read-only memory.

    >
    > It's less common than you might think.


    ....in desk top and workstation type implementations. It is very
    common in embedded implementation, so much so in fact that I would
    seriously consider rejecting a compiler that copied string literals
    from read-only storage to RAM by default.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Apr 6, 2004
    #9
  10. Wade Yin

    Dan Pop Guest

    In <> Jack Klein <> writes:

    >On 5 Apr 2004 15:31:17 GMT, (Dan Pop) wrote in
    >comp.lang.c:
    >
    >> In <c4rlhk$8gh$> Christopher Benson-Manica <> writes:
    >>
    >> >Wade Yin <> spoke thus:
    >> >
    >> >> How does "abcde" stored in memory?
    >> >
    >> >Exactly how is (I believe) up to the implementation, but commonly it
    >> >ends up in read-only memory.

    >>
    >> It's less common than you might think.

    >
    >...in desk top and workstation type implementations.


    I.e. in what the standard calls hosted implementations, which happen to
    be the default implementations for this newsgroup.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 6, 2004
    #10
  11. Dan Pop wrote:

    > Wade Yin writes:
    >
    >>What's the difference between:
    >>
    >> char *x="abcde";
    >>
    >>and
    >>
    >> char y[]={"abcde"};

    >
    > How about reading the FAQ *before* posting?


    Why would Wade read the [C] FAQ *before* posting?
    Does the FAQ answer this question?
    Can you cite and quote the relevant FAQ?
     
    E. Robert Tisdale, Apr 6, 2004
    #11
  12. "E. Robert Tisdale" <> a écrit dans le message
    de news:...

    Hi,

    > Does the FAQ answer this question?


    Yes.

    > Can you cite and quote the relevant FAQ?


    The 1.32 FAQ answers to the Wade's question.
    http://www.eskimo.com/~scs/C-faq/q1.32.html

    Regis
     
    Régis Troadec, Apr 6, 2004
    #12
  13. Wade Yin

    Wade Yin Guest

    On 5 Apr 2004 14:33:03 GMT, (Dan Pop) wrote:

    >
    >How about reading the FAQ *before* posting?


    Sure, most of things can be deal with by FAQ,

    But It's too long to fit for a single question, Right?!

    >
    >Dan
     
    Wade Yin, Apr 7, 2004
    #13
  14. Wade Yin

    Wade Yin Guest


    >On Tue, 6 Apr 2004 19:14:48 +0200, "Régis Troadec" <> wrote:


    >
    >"E. Robert Tisdale" <> a ?crit dans le message
    >de news:...
    >
    >Hi,
    >
    >> Does the FAQ answer this question?

    >
    >Yes.
    >
    >> Can you cite and quote the relevant FAQ?

    >
    >The 1.32 FAQ answers to the Wade's question.
    >http://www.eskimo.com/~scs/C-faq/q1.32.html
    >


    Cool...

    I should read it carefully after this question....

    But sometime you can't find it in time, so you have to ask at here..:(

    So please keep patient...

    Thank you all.

    You gave me very good help...
     
    Wade Yin, Apr 7, 2004
    #14
  15. Wade Yin

    Richard Bos Guest

    "E. Robert Tisdale" <> wrote:

    > Dan Pop wrote:
    >
    > > How about reading the FAQ *before* posting?

    >
    > Why would Wade read the [C] FAQ *before* posting?


    Because it is nothing but polite to read the FAQ for _any_ newsgroup you
    intend to start posting to. So is reading a reasonable amount of
    backlog. It's basic netiquette, really. Which explains why it's done so
    rarely :-(

    Richard
     
    Richard Bos, Apr 7, 2004
    #15
  16. Wade Yin

    anonymous Guest

    The FAQ shows the declaration like:

    char a[] = "string literal";

    But in the OP's question it is like:

    char y[] = {"string literal"};

    Are they same?
     
    anonymous, Apr 7, 2004
    #16
  17. Wade Yin

    Dan Pop Guest

    In <> "E. Robert Tisdale" <> writes:

    >Dan Pop wrote:
    >
    >> Wade Yin writes:
    >>
    >>>What's the difference between:
    >>>
    >>> char *x="abcde";
    >>>
    >>>and
    >>>
    >>> char y[]={"abcde"};

    >>
    >> How about reading the FAQ *before* posting?

    >
    >Why would Wade read the [C] FAQ *before* posting?
    >Does the FAQ answer this question?


    Yup. In "graphic" detail.

    >Can you cite and quote the relevant FAQ?


    Yup, but only if you openly admit that you cannot find it yourself.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 7, 2004
    #17
  18. Wade Yin

    Dan Pop Guest

    In <> Wade Yin <> writes:

    >On 5 Apr 2004 14:33:03 GMT, (Dan Pop) wrote:
    >
    >>
    >>How about reading the FAQ *before* posting?

    >
    >Sure, most of things can be deal with by FAQ,
    >
    >But It's too long to fit for a single question, Right?!


    Wrong! The FAQ is divided into sections so that you don't have to
    read the parts that are obviously irrelevant to your question. And your
    question is the second in the section... Arrays and Pointers, i.e. the
    very place where an intelligent person would have searched the answer.

    So, you have NO excuse for not doing your homework before posting...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 7, 2004
    #18
  19. Wade Yin

    Dan Pop Guest

    In <> (anonymous) writes:

    >The FAQ shows the declaration like:
    >
    >char a[] = "string literal";
    >
    >But in the OP's question it is like:
    >
    >char y[] = {"string literal"};
    >
    >Are they same?


    Why would they be any different? What difference do you expect those
    superfluous braces to make?

    FYI:

    int i = {1};

    is also allowed. I hope you're not going to ask me if it's the same as

    int i = 1;

    ;-)

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Apr 7, 2004
    #19
  20. Wade Yin

    CBFalconer Guest

    Richard Bos wrote:
    > "E. Robert Tisdale" <> wrote:
    >> Dan Pop wrote:
    >>
    >>> How about reading the FAQ *before* posting?

    >>
    >> Why would Wade read the [C] FAQ *before* posting?

    >
    > Because it is nothing but polite to read the FAQ for _any_
    > newsgroup you intend to start posting to. So is reading a
    > reasonable amount of backlog. It's basic netiquette, really.
    > Which explains why it's done so rarely :-(



    +-------------------+ .:\:\:/:/:.
    | PLEASE DO NOT | :.:\:\:/:/:.:
    | FEED THE TROLLS | :=.' - - '.=:
    | | '=(\ 9 9 /)='
    | Thank you, | ( (_) )
    | Management | /`-vvv-'\
    +-------------------+ / \
    | | @@@ / /|,,,,,|\ \
    | | @@@ /_// /^\ \\_\
    @x@@x@ | | |/ WW( ( ) )WW
    \||||/ | | \| __\,,\ /,,/__
    \||/ | | | jgs (______Y______)
    /\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    ==============================================================

    Trollsdale only does it to annoy.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Apr 7, 2004
    #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. jakk
    Replies:
    4
    Views:
    12,397
  2. ppcguy
    Replies:
    5
    Views:
    454
    Pete Gray
    Aug 20, 2005
  3. Guybrush Threepwood
    Replies:
    7
    Views:
    522
    Nick Hounsome
    Feb 26, 2004
  4. pmatos
    Replies:
    11
    Views:
    669
    Rolf Magnus
    Mar 17, 2005
  5. Deniz Bahar
    Replies:
    2
    Views:
    492
    Andrey Tarasevich
    Mar 9, 2005
Loading...

Share This Page