#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
  char *a = "Hello ";
  const char *b = "World";
  printf("%s", strcat(a, b));
  system("PAUSE");
  return EXIT_SUCCESS;
}
            Asked
            
        
        
            Active
            
        
            Viewed 128 times
        
    1
            
            
         
    
    
        unwind
        
- 391,730
- 64
- 469
- 606
 
    
    
        Amol Aggarwal
        
- 2,674
- 3
- 24
- 32
- 
                    3String literals are not modifyable. http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault/1614739#1614739 – AnT stands with Russia Jan 14 '10 at 08:19
2 Answers
7
            Because you are writing data at a memory location that you do not own.
Indeed, when running strcat, you are appending the characters of string b right after the characters of string a. But you haven't claimed for the memory after the string a.
 
    
    
        Didier Trosset
        
- 36,376
- 13
- 83
- 122
 
    