where wrong?how to correct?and why? thank you

Discussion in 'C Programming' started by wwj, Nov 7, 2003.

  1. wwj

    wwj Guest

    void main()
    {
    char* p="Hello";
    printf("%s",p);
    *p='w';
    printf("%s",p);
    }
     
    wwj, Nov 7, 2003
    #1
    1. Advertising

  2. wwj <> scribbled the following:
    > void main()


    This is a non-standard form of main(). There is no guarantee it will
    work. Better use: int main(void)

    > {
    > char* p="Hello";
    > printf("%s",p);
    > *p='w';
    > printf("%s",p);
    > }


    The assignment *p='w' modifies a string literal, causing undefined
    behaviour. On most implementations, string literals reside in read-only
    memory. Thus *p='w' can cause a segfault or something similar.
    Try instead: char p[6]="Hello";

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "This is a personnel commuter."
    - Train driver in Scientific American
     
    Joona I Palaste, Nov 7, 2003
    #2
    1. Advertising

  3. In article <>, wwj wrote:

    Missing inclusion of <stdio.h>.

    > void main()


    int main(void)

    > {
    > char* p="Hello";
    > printf("%s",p);
    > *p='w';


    You're not allowed to change a string literal.

    > printf("%s",p);


    Missing flushing of stdout and "return 0;".

    > }


    Corrected:

    #include <stdio.h>

    int main(void)
    {
    char p[] = "Hello";
    printf("%s\n", p);
    p[0] = 'w';
    printf("%s\n", p);
    return 0;
    }


    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 7, 2003
    #3
  4. On 7 Nov 2003 06:37:44 -0800, (wwj)
    wrote:

    >void main()


    int main(void) !!!!!!

    >{
    > char* p="Hello";


    char *p = "Hello";
    Don't let char* fool you, keep the * on the variable.

    > printf("%s",p);


    printf("%s\n", p);

    > *p='w';


    No! You cannot write to the string literal, "Hello".

    > printf("%s",p);


    Again, you forgot the \n newline char.


    return 0;
    >}
     
    Mark A. Odell, Nov 7, 2003
    #4
  5. Mark A. Odell <> scribbled the following:
    > On 7 Nov 2003 06:37:44 -0800, (wwj)
    > wrote:
    >>{
    >> char* p="Hello";


    > char *p = "Hello";
    > Don't let char* fool you, keep the * on the variable.


    Note to newbies: This is an entirely stylistic correction and will
    not affect the produced program at all.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It was, er, quite bookish."
    - Horace Boothroyd
     
    Joona I Palaste, Nov 7, 2003
    #5
  6. Re: Re: where wrong?how to correct?and why? thank you

    On 7 Nov 2003 14:46:45 GMT, Joona I Palaste <>
    wrote:

    >>>{
    >>> char* p="Hello";

    >
    >> char *p = "Hello";
    >> Don't let char* fool you, keep the * on the variable.

    >
    >Note to newbies: This is an entirely stylistic correction and will
    >not affect the produced program at all.


    Unless you write:

    char* pFoo, pBar, pBaz;

    and expect to get three pointers to char.
     
    Mark A. Odell, Nov 7, 2003
    #6
  7. Mark A. Odell <> scribbled the following:
    > On 7 Nov 2003 14:46:45 GMT, Joona I Palaste <>
    > wrote:
    >>>>{
    >>>> char* p="Hello";

    >>
    >>> char *p = "Hello";
    >>> Don't let char* fool you, keep the * on the variable.

    >>
    >>Note to newbies: This is an entirely stylistic correction and will
    >>not affect the produced program at all.


    > Unless you write:


    > char* pFoo, pBar, pBaz;


    > and expect to get three pointers to char.


    That is true, but what I was talking about was strictly what you wrote
    in "correction" of the OP's code. The definition above is equivalent
    to:

    char *pFoo, pBar, pBaz;

    or to:
    char
    * pFoo
    , pBar ,
    pBaz ;

    What matters is how many * there are and between which words they
    occur. Whether they "bind" to the type words or the variable name
    words is irrelevant.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
    - ALF
     
    Joona I Palaste, Nov 7, 2003
    #7
  8. Greetings.

    In article <>, wwj wrote:

    > void main()


    int main(void)

    > {
    > char* p="Hello";
    > printf("%s",p);


    You forgot to #include <stdio.h>.

    > *p='w';


    You are not premitted to overwrite a string literal, which is what p points
    to. You could solve this problem by making p and array and initializing it
    with the string "hello":

    char p[6] = "Hello";

    (The array length is 6 because C requires strings to contain a special
    end-of-string marker, or "null character".)

    > printf("%s",p);


    Since this is the last output of the program, it needs to be followed by a
    newline. Otherwise you invoke undefined behaviour. Try:

    printf("%s\n", p);

    > }


    You have ended main without returning a value. The preferred method for an
    exit without errors is:

    return EXIT_SUCCESS;

    The EXIT_SUCCESS macro is #defined in the <stdlib.h> header, which you must
    #include before you use it. Alternatively, you could just write

    return 0;

    Regards,
    Tristan

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
     
    Tristan Miller, Nov 7, 2003
    #8
  9. wwj

    Dan Pop Guest

    In <bogb8l$sh9$> Joona I Palaste <> writes:

    >Mark A. Odell <> scribbled the following:
    >> On 7 Nov 2003 06:37:44 -0800, (wwj)
    >> wrote:
    >>>{
    >>> char* p="Hello";

    >
    >> char *p = "Hello";
    >> Don't let char* fool you, keep the * on the variable.

    >
    >Note to newbies: This is an entirely stylistic correction and will
    >not affect the produced program at all.


    It will, once the newbie writes:

    char* p, q;

    and expects the declaration to have the "obvious" meaning.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Nov 7, 2003
    #9
  10. wwj

    Dan Pop Guest

    In <> (wwj) writes:

    >void main()


    Here wrong.

    >{
    > char* p="Hello";
    > printf("%s",p);


    Here wrong.

    > *p='w';


    Here wrong.

    > printf("%s",p);


    Here wrong.

    >}


    Congratulations! With the exception of the braces, you got a single line
    of code right, and even that line is not adequate for your purposes.

    Do yourself a favour and read a C book.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Nov 7, 2003
    #10
  11. wwj

    wwj Guest

    On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:

    Do you actually contribute anything here, or do you just repost advice that
    others have already given?

    > In <bogb8l$sh9$> Joona I Palaste
    > <> writes:
    >
    >>Mark A. Odell <> scribbled the following:
    >>> On 7 Nov 2003 06:37:44 -0800, (wwj)
    >>> wrote:
    >>>>{
    >>>> char* p="Hello";

    >>
    >>> char *p = "Hello";
    >>> Don't let char* fool you, keep the * on the variable.

    >>
    >>Note to newbies: This is an entirely stylistic correction and will
    >>not affect the produced program at all.

    >
    > It will, once the newbie writes:
    >
    > char* p, q;
    >
    > and expects the declaration to have the "obvious" meaning.
    >
    > Dan
     
    wwj, Nov 7, 2003
    #11
  12. wwj <> scribbled the following:
    > On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:
    > Do you actually contribute anything here, or do you just repost advice that
    > others have already given?


    You're new here, aren't you? Dan Pop is one of the most clueful people
    here. He is not always polite but he is nearly always right. He has
    answered a great deal of questions here, even more than I have. Just
    because he reposts *one* piece of advice that's already been given
    doesn't mean he never contributes anything.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Stronger, no. More seductive, cunning, crunchier the Dark Side is."
    - Mika P. Nieminen
     
    Joona I Palaste, Nov 7, 2003
    #12
  13. wwj <> wrote:

    [top-posting fixed; please don't top-post; thank you]

    > On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:
    > > In <bogb8l$sh9$> Joona I Palaste
    > > <> writes:
    > >
    > >>Mark A. Odell <> scribbled the following:
    > >>> On 7 Nov 2003 06:37:44 -0800, (wwj)
    > >>> wrote:
    > >>>>{
    > >>>> char* p="Hello";
    > >>
    > >>> char *p = "Hello";
    > >>> Don't let char* fool you, keep the * on the variable.
    > >>
    > >>Note to newbies: This is an entirely stylistic correction and will
    > >>not affect the produced program at all.

    > >
    > > It will, once the newbie writes:
    > >
    > > char* p, q;
    > >
    > > and expects the declaration to have the "obvious" meaning.

    >
    > Do you actually contribute anything here, or do you just repost advice that
    > others have already given?


    I'd like to ask you some questions:

    Do you actually now how usenet works? Do you now what off-line reading
    means? Did you ever notice that propagation of messages is a non-linear
    process? What is wrong with two people making a similar remark,
    especially if it's correct? Did you ever read any more messages by Dan
    Pop than the one you just responded to?

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Nov 7, 2003
    #13
  14. On Fri, 07 Nov 2003 16:22:07 +0000, wwj wrote:

    > On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:
    >
    > Do you actually contribute anything here, or do you just repost advice that
    > others have already given?


    No, I don't think "Dan Pop" actually wrote that.
    Please attribute correctly and don't top post. Thanks.
     
    Sheldon Simms, Nov 7, 2003
    #14
  15. wwj

    CBFalconer Guest

    *** Rude top-posting corrected ***

    wwj wrote:
    > On Fri, 07 Nov 2003 15:23:03 GMT, "Dan Pop" said:
    > > Joona I Palaste <> writes:
    > >> Mark A. Odell <> scribbled the following:
    > >>> (wwj) wrote:
    > >>>>
    > >>>>{
    > >>>> char* p="Hello";
    > >>
    > >>> char *p = "Hello";
    > >>> Don't let char* fool you, keep the * on the variable.
    > >>
    > >> Note to newbies: This is an entirely stylistic correction and
    > >> will not affect the produced program at all.

    > >
    > > It will, once the newbie writes:
    > >
    > > char* p, q;
    > >
    > > and expects the declaration to have the "obvious" meaning.

    >
    > Do you actually contribute anything here, or do you just repost
    > advice that others have already given?


    Congratulations. You have just rudely contravened standard
    practice here by top-posting, and gratuitously insulted one of the
    more knowledgeable participants, who in turn is not noted for his
    gentle handling of fools, nor for his diplomatic skills. You
    contrived to do all this in only two lines. This is a well tested
    method of attracting future help.

    In the likely case that you are too mentally challenged to
    recognize it, you have just been flamed and insulted. A
    productive response would be abject apology and grovelling.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Nov 7, 2003
    #15
  16. On Fri, 7 Nov 2003, CBFalconer wrote:

    [Talking about Dan Pop.]

    > one of the
    > more knowledgeable participants, who in turn is not noted for his
    > gentle handling of fools, nor for his diplomatic skills.


    I think his diplomatic skills have improved significantly
    during the past two years. :)

    Tak-Shing, biased sampling
     
    Tak-Shing Chan, Nov 7, 2003
    #16
  17. wwj

    nobody Guest

    "Tristan Miller" <> wrote in message
    news:...
    > Greetings.
    >
    > In article <>, wwj wrote:
    >
    > > void main()

    >
    > int main(void)
    >
    > > {
    > > char* p="Hello";
    > > printf("%s",p);

    >
    > You forgot to #include <stdio.h>.
    >
    > > *p='w';

    >
    > You are not premitted to overwrite a string literal, which is what p

    points
    > to. You could solve this problem by making p and array and initializing

    it
    > with the string "hello":
    >
    > char p[6] = "Hello";
    >
    > (The array length is 6 because C requires strings to contain a special
    > end-of-string marker, or "null character".)


    That's all true, but why to count (and eventually not re-count)
    if compiler does the grunt work? Right, Joona?

    char p[] = "Hello";

    > > printf("%s",p);

    >
    > Since this is the last output of the program, it needs to be followed by a
    > newline. Otherwise you invoke undefined behaviour.


    Undefined? It's not even unintended. *Because* it is
    "last output of the program".
    [N869]
    7.19.3 Files
    ....
    5 ... If the main function returns to its original caller,
    or if the exit function is called, all open files are
    closed (hence all output streams are flushed) before
    program termination. Other paths to program termination,
    such as calling the abort function, need not close all
    files properly.

    [snip]
     
    nobody, Nov 9, 2003
    #17
  18. wwj

    pete Guest

    nobody wrote:
    >
    > "Tristan Miller" <> wrote in message
    > news:...
    > > Greetings.
    > >
    > > In article <>,
    > > wwj wrote:


    > > > printf("%s",p);

    > >
    > > Since this is the last output of the program,
    > > it needs to be followed by a newline.
    > > Otherwise you invoke undefined behaviour.

    >
    > Undefined?


    N869
    7.19.2 Streams
    [#2]
    Whether the last line requires
    a terminating new-line character
    is implementation-defined.

    --
    pete
     
    pete, Nov 9, 2003
    #18
  19. wwj

    nobody Guest

    "pete" <> wrote in message
    news:...
    > nobody wrote:
    > >
    > > "Tristan Miller" <> wrote in message
    > > news:...
    > > > Greetings.
    > > >
    > > > In article <>,
    > > > wwj wrote:

    >
    > > > > printf("%s",p);
    > > >
    > > > Since this is the last output of the program,
    > > > it needs to be followed by a newline.
    > > > Otherwise you invoke undefined behaviour.

    > >
    > > Undefined?

    >
    > N869
    > 7.19.2 Streams
    > [#2]
    > Whether the last line requires
    > a terminating new-line character
    > is implementation-defined.
    >

    So ... does it mean that absence of '\n' invokes undefined
    behaviour? Or it's presence? From your citation then, it would
    seem that whatever you do, you can't have truly portable program,
    if you are sending "data" to stdout/stderr. But I must be wrong,
    so what did I miss? Why was my citation irrelevant to the case?
     
    nobody, Nov 10, 2003
    #19
  20. wwj

    pete Guest

    nobody wrote:
    >
    > "pete" <> wrote in message
    > news:...
    > > nobody wrote:
    > > >
    > > > "Tristan Miller" <> wrote in message
    > > > news:...
    > > > > Greetings.
    > > > >
    > > > > In article <>,
    > > > > wwj wrote:

    > >
    > > > > > printf("%s",p);
    > > > >
    > > > > Since this is the last output of the program,
    > > > > it needs to be followed by a newline.
    > > > > Otherwise you invoke undefined behaviour.
    > > >
    > > > Undefined?

    > >
    > > N869
    > > 7.19.2 Streams
    > > [#2]
    > > Whether the last line requires
    > > a terminating new-line character
    > > is implementation-defined.
    > >

    > So ... does it mean that absence of '\n' invokes undefined
    > behaviour?


    It means that if the stream is not terminated with a newline,
    that the code is not portable.
    On a system which defines a stream as ending in a newline,
    if you have something that's just like a stream,
    except for the way that it's terminated, then it's not a stream,
    and it isn't defined.

    > Or it's presence? From your citation then, it would
    > seem that whatever you do, you can't have truly portable program,
    > if you are sending "data" to stdout/stderr.


    All you have to do, is make sure that the last output character
    to the standard output stream, is a null character, to be portable.

    > But I must be wrong,
    > so what did I miss? Why was my citation irrelevant to the case?


    Because it has nothing to do with whether or not the last output
    character in a text stream should be a null character.

    --
    pete
     
    pete, Nov 10, 2003
    #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. Miguel Dias Moura
    Replies:
    1
    Views:
    431
    Lars Netzel
    Jun 18, 2004
  2. Miguel Dias Moura
    Replies:
    4
    Views:
    3,321
    Miguel Dias Moura
    Jun 23, 2004
  3. Miguel Dias Moura
    Replies:
    1
    Views:
    439
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  5. shapper
    Replies:
    3
    Views:
    336
    Roland Dick
    Mar 14, 2007
Loading...

Share This Page