error ..simple array

Discussion in 'C Programming' started by gNash, Apr 23, 2007.

  1. gNash

    gNash Guest

    main()
    {
    char arr[20]="hai";

    strcat(arr,arr);

    printf("%s",arr);
    }

    its printed haihai..

    when i replace hai to haii..

    i got segmentation fault..

    Why?...
     
    gNash, Apr 23, 2007
    #1
    1. Advertising

  2. gNash

    Flash Gordon Guest

    gNash wrote, On 23/04/07 12:33:
    > main()
    > {
    > char arr[20]="hai";
    >
    > strcat(arr,arr);
    >
    > printf("%s",arr);
    > }
    >
    > its printed haihai..
    >
    > when i replace hai to haii..
    >
    > i got segmentation fault..
    >
    > Why?...


    Blind luck. Passing overlapping strings to strcat invokes undefined
    behaviour, which means that as far as the C language is concerned
    ANYTHING is allowed to happen. You happen to have had the most common
    results with your two tries, but just as well have caused your computer
    to show hard porn whenever your mother or partner looks at it.
    --
    Flash Gordon
     
    Flash Gordon, Apr 23, 2007
    #2
    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. Replies:
    14
    Views:
    548
    Karl Heinz Buchegger
    Nov 2, 2005
  2. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    505
    David RF
    Sep 3, 2009
  3. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    219
    Peter Bailey
    Apr 8, 2008
  4. RichardOnRails
    Replies:
    3
    Views:
    137
    RichardOnRails
    Jul 21, 2008
  5. Tom
    Replies:
    3
    Views:
    219
    salsablr
    Dec 20, 2004
Loading...

Share This Page