Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > error ..simple array

Reply
Thread Tools

error ..simple array

 
 
gNash
Guest
Posts: n/a
 
      04-23-2007

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?...

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      04-23-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments