Like Tree1Likes
  • 1 Post By Salem

Segmentation Fault (core dumped) in simple c program !

This is a discussion on Segmentation Fault (core dumped) in simple c program ! within the C Programming forums, part of the General Programming Boards category; Hello there, I am getting the error: Code: Segmentation fault (core dumped) error in a simple c program. The code ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    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.
    rags_to_riches likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10:06 AM
  3. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 02: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, 02:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21