Like Tree1Likes
  • 1 Post By anduril462

Segmentation fault (core dumped) message

This is a discussion on Segmentation fault (core dumped) message within the C Programming forums, part of the General Programming Boards category; I keep finding this message when i run my Caesar Cipher code. It complies, but then displays this message. I ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    7

    Segmentation fault (core dumped) message

    I keep finding this message when i run my Caesar Cipher code.
    It complies, but then displays this message. I can't seem to find an answer to this problem anywhere as all the other posts on this topic don't seem to have any resemblence to my problem

    My code is:

    Code:
    /* The program encrypts a string using the caesar cypher*/
    #include<cs50.h>
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<string.h>
     
    int main(int argc, char *argv[])
    {
          // check if the argument entered is a number
          if (!isdigit(*argv[1]) || argc !=2)
          {
                printf("Please enter one number as the argument to use to encrypt\n");
                return 1;
          }
          // convert the argument to an int
          int k = atoi(argv[1]);
          // get the user's plaintext to encrypt
          printf("\nplaintext:   ");
          char *plain = GetString();
          // encrypt
          printf("\ncyphertext:  ");
          for (int i = 0, n = strlen(plain); i < n; i++)
          {
                // if the letters are lowercase
                if(plain[i] > 64 && plain[i] < 91)
                {
                      int ascii = plain[i];
                      int p = (ascii - 65);
                      int c = (p + k) % 26;
                      int cypher_p = (c + 65);
                      printf("%c", cypher_p);
                }
                // if the letters are uppercase
                 // this is where the error is
                else if(plain[i] > 96 && plain[i] < 123)
                {
                      int ascii = plain[i];
                      int p = (ascii - 97);
                      int c = (p + k) % 26;
                      int cypher_p = (c + 97);
                      printf("%c", cypher_p);
                }
                // if the element in plain is a space print a space
                else if(strcmp(&plain[i], " ") == 0)
                {
                      printf(" ");
                }
          }
          printf("\n");
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    First welcome to the forum

    Second you should always check, via argc that you have the required number of arguments!

    Third I guess you should allocate some space for your char pointer! (plain)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Thanks for your quick reply.

    Im starting out in C and i am really struggling, its taken me maybe 12 hours of banging my head against the wall to get this far.
    My mind is so utterly closed now even your hint doesn't make sense to me
    Care to go a little further in your explaination?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    If you don't provide any arguments on the command line, argc will have a value of 1, and argv[1] will be the NULL pointer. Computing isdigit(*argv[1]) attempts to access memory via that NULL pointer (that is also called "dereferencing the NULL pointer"). That is an invalid memory access.

    Turn the test "!isdigit(*argv[1]) || argc !=2" into "argc !=2 || !isdigit(*argv[1])" for starters. Thanks to short-circuit evaluation, argc != 2 will be checked first, and !isdigit(*argv[1]) will only be evaluated if argc has a value 2. That avoids dereferencing the NULL pointer.

    I haven't looked any further than that, but that's hopefully a start..
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Thank you Grumpy, i see where i went wrong there now

    I have lost the Segmentation fault message but now the programme sticks at the printf function
    printf("Please enter one number as the argument to use to encrypt\n");

    The quest continues...

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by pye168 View Post
    I have lost the Segmentation fault message but now the programme sticks at the printf function
    printf("Please enter one number as the argument to use to encrypt\n");
    No wonder because you exit the program after printing:
    Code:
    if (!isdigit(*argv[1]) || argc !=2)
    {
         printf("Please enter one number as the argument to use to encrypt\n");
        return 1;
    }
    Do you understand how to use this program?

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    If i understood how to use it then i wouldn't be relying on the help of strangers would i?

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    and in response to your unhelpful answer on the other thread, this isnt the same problem as i was having before....

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by pye168 View Post
    If i understood how to use it then i wouldn't be relying on the help of strangers would i?
    It's a little bit strange that you write a program using command line arguments but don't know how to use it. So I wonder if you have written the program yourself.

    Look at this:
    Code:
    $ ./foo
    Please enter one number as the argument to use to encrypt
    $ ./foo 1
    
    plaintext:   hello
    
    cyphertext:  ifmmp
    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,325
    Quote Originally Posted by AndiPersti View Post
    It's a little bit strange that you write a program using command line arguments but don't know how to use it. So I wonder if you have written the program yourself.
    I guess that's because he didn't write it: http://cboard.cprogramming.com/c-pro...fore-else.html. At least you have to give him credit for searching the forum before posting
    Quote Originally Posted by pye168 View Post
    I keep finding this message when i run my Caesar Cipher code.
    (emphasis mine)
    You realize that claiming somebody else's work as your own is plagiarism -- it's dishonest at the least, and outright theft at the worst. We don't care for that kind of behavior.

    The other problem with using code you didn't write, especially when you're new to programming, is that you don't understand how it works. That means you can't understand what is wrong when it doesn't work, and you just don't learn anything except how to copy code off the internet.

    Have the decency to give credit to those who actually did the work. Besides, if you told us that you got the code from somewhere else, and linked us to that code, we could better help you. We could tell you whether the code was any good to begin with, whether you copied something incorrectly or what. As it happens, you didn't seem to grasp all the points we made to BigWigly in his thread. You got some, but not all.
    AndiPersti likes this.

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    Im not trying to take ownership of anyones code. Im just trying to understand the thing and as i am trying to do it off my own back then obviously i have to find help in the resources of others. Isn't that what learning is? I didnt utterly copy and paste this guys code, but admittedly i did try to learn from it. But from where else can i start?
    Im not submitting this for any grade or for any prize.
    Thanks for your comments

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,325
    Quote Originally Posted by pye168 View Post
    Im not trying to take ownership of anyones code.
    Yet you used the term "my Caesar Cipher code". That sounds like you're saying you own it, or created it, yet the code is clearly not yours.
    Quote Originally Posted by pye168 View Post
    Im just trying to understand the thing and as i am trying to do it off my own back then obviously i have to find help in the resources of others. Isn't that what learning is?
    Yes, reading and trying to understand something is one form of learning. However, if you barely know arithmetic, then trying to read a calculus book would be a waste of time. Not because you can't learn, you're just not ready for it yet, you lack the foundation. I am not knocking you for trying to learn. Doing this on your own, however, from scratch, would be much more beneficial and you would learn much more. Also you would avoid being stuck on some simple logic you don't understand simply because you didn't write it. If you're not doing this yourself because you find it too hard, then work some simpler examples until you think this is within your reach.
    Quote Originally Posted by pye168 View Post
    I didnt utterly copy and paste this guys code
    There is only one functional difference (though it is still incorrect) between the code you posted, and the code you copied. The rest is just insignificant changes in comments or printf strings. I think by most people's definition, that would constitute utter copy-pasting. The hard parts, the logic to perform the cipher, etc, are 100% copied. Unfortunately, those are the most important parts, because learning to figure those parts out on your own develops your problem solving skills, and those skills are what are most important in programming. Programming is ultimately about problem solving, writing code is merely the tool used to solve the problem. If you can't solve the problem in your mind, or on paper, then you can never write a program to do it, no matter how good your C skills.
    Quote Originally Posted by pye168 View Post
    But from where else can i start?
    Perhaps find some tutorials or books that start with simpler examples. If you're having trouble following basic if-else logic, like what Andreas pointed out in post #6, then I would hold off on this Caesar Cipher program until you have mastered basic flow control structures like if/else and loops, both of which are necessary for this program.

    All that aside, really, we all would like you to learn, and we would like to help you, but this is certainly not the best way. I suggest you throw this code away. It's not total rubbish, but it's also not the best code to learn from. Go back to the basics if you need help understanding if/else and loops, and strings and arrays. Work through as many examples as you need to really understand those concepts. Then, when you have that down, come back to this problem, but implement it entirely yourself. If you get stuck, ask us for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By TimI in forum C Programming
    Replies: 4
    Last Post: 11-05-2012, 06:45 AM
  2. Segmentation fault (core dumped)
    By benjaminp in forum C Programming
    Replies: 8
    Last Post: 10-10-2012, 12:46 PM
  3. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 06:37 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  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