Thread: Segmentation Fault (core dumped) in simple c program !

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Lightbulb Segmentation Fault (core dumped) in simple c program !

    Hello there,

    I am getting the error:
    Code:
    Segmentation fault (core dumped)
    error in a simple c program. The code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char*
    encrypt(char *s)
    {
      int i, l = strlen(s);
      for(i = 0; i < l; i++)
          s[i] -= 15;
      
      return s;
    }
    
    
    int main(int argc, char *argv[])
    {
        char* test = "This is simple encryption.";
        
        printf("%s", encrypt(test));
      return 0;
    }
    Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try it with
    char test[] = "This is simple encryption.";

    Then understand that in your case, your pointer was pointing to constant read-only memory, and that trying to modify it results in instant death for your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault (core dumped)?
    By tkd_aj in forum C Programming
    Replies: 17
    Last Post: 04-18-2012, 01:53 AM
  2. Segmentation fault (core dumped)
    By mrbotts in forum C Programming
    Replies: 2
    Last Post: 01-10-2012, 11:06 AM
  3. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 03:56 PM
  4. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread