Thread: Segmentation Error

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

    Segmentation Error

    Hi,
    I am writing a encryption problem that uses ceasar cyphering system. User is meant to enter in the command line the programme, string and a key that will determing how far the characters should be moved in the ASCII chart. My program is returning a segmetation error. Please help to identify the error

    Code:
    # include <stdio.h>
    
    
    # include <cs50.h>
    
    
    # include <stdlib.h>
    
    
    # include <string.h>
    
    
    # include <ctype.h>
    
    
    
    
    int main(int argc, string argv[])
    {
        //command line to have three entries or else  quit
        if (argc !=3)
            return 1;
    
        {
            string s = argv[1];
    
            //int length = strlen(s);
    
    
    
    
    
            //iterate through the string
            for (int i = 0; i < strlen(s); i++)
                {
                    //char kar = s[i];
    
                    int ch = 's[i]';
    
    
                    int key = atoi(argv[2]);
    
                    if ( isalpha("ch"))
                    {
                        bool upper = true;
                        do
                        {
    
                            int value_u = (ch + key);
                            int result_u;
                            int Z = 'Z';
                            {
                                if (value_u > Z)
                                {   
                                    result_u = value_u % 26;
    
                                    printf("%c",result_u);  
                                 }
                                else
    
                                    printf("%c",result_u);
                            }               
                            }
                            while (upper == false);
                            {
                                int value_l = (ch + key);
                                int result_l;
                                int z = 'z';
                                {
                                    if (value_l > z)
                                    {   
                                        result_l = value_l % 26;
    
                                        printf("%c",result_l);  
                                     }
                                    else
    
                                        printf("%c",result_l);
                                }
                            }
                        }
                        else
                            printf("%c",ch);
                    }
                printf("\n");
        }
        return 0;                
    }
    

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That code won't compile due to invalid constructs like
    Code:
        int ch = 's[i]';
    and
    Code:
     if ( isalpha("ch"))
    There is also no type in C called string.

    Fix the problems in your code that cause it not to compile. A program cannot produce runtime errors (such as segmentation violations, which I assume is what you mean by "segmentation error") if it cannot be executed. Successful compilation is a precursor to even having an executable.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Its also a very convoluted way of accomplishing the task - you could just use an array with the alphabet in it and then compare the contents of the message passed into the program match the letter to the alphabet array and then shift the index (remembering to wrap around) by whatever the key value is to obtain the ciphertext
    This may seem convoluted too to some, but thats how i picture it
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by grumpy View Post
    There is also no type in C called string.
    From here http://cdn.cs50.net/2011/fall/lectures/4/src4/cs50.h
    I would guess someone had a "bright" idea to type def an char* as string.

    Code:
    typedef char *string;
    I wonder when I learned things like that do NOT help.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    From here http://cdn.cs50.net/2011/fall/lectures/4/src4/cs50.h
    I would guess someone had a "bright" idea to type def an char* as string.

    Code:
    typedef char *string;
    I wonder when I learned things like that do NOT help.
    Either way, not exactly standard. The most common origin of tricks like that is a mistaken belief that a pointer is an array (from which it is a small step to believing a char * is a string). As you say, such tricks cause a lot of trouble, for negligible benefits.

    The other two problems in the code I pointed out remain extant.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by grumpy View Post
    That code won't compile due to invalid constructs like
    Code:
        int ch = 's[i]';
    That one will compile just fine. It's a multicharacter constant. The value it assigns is implemenatation defined I believe, and on my system it gives the value of 0x735B695D.
    It's probably not what was intended though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by iMalc View Post
    That one will compile just fine. It's a multicharacter constant. The value it assigns is implemenatation defined I believe, and on my system it gives the value of 0x735B695D.
    It's probably not what was intended though.
    Indeed -- g++ compiles it with a warning:
    warning: multi-character character constant [-Wmultichar]

    So I tried running the program, and this does indeed seem to be the problem.

    From http://pubs.opengroup.org/onlinepubs.../isalpha.html:
    The c argument is an int, the value of which the application shall ensure is representable as an unsigned char or equal to the value of the macro EOF. If the argument has any other value, the behavior is undefined.
    So although I'm a little surprised that it segfaults, it's not wrong to do so. Probably the implementation involves a lookup table of properties.

    I ran it with valgrind before I clicked that the multibyte char was the problem -- there are other problems with this code too:
    Code:
                        do
                        {
    
                            int value_u = (ch + key);
                            int result_u;
                            int Z = 'Z';
                            {
                                if (value_u > Z)
                                {   
                                    result_u = value_u % 26;
    
                                    printf("%c",result_u);  
                                 }
                                else
    
                                    printf("%c",result_u);
                            }               
                            }
    In the printf in the else statement, result_u could be uninitialised.

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    ...and seemingly from Harvard University no less. smh. Wow.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Error
    By Native in forum C Programming
    Replies: 2
    Last Post: 10-26-2010, 08:41 AM
  2. Segmentation Error
    By mrdibby in forum C Programming
    Replies: 4
    Last Post: 12-06-2009, 04:25 AM
  3. I get segmentation error
    By iLike in forum C Programming
    Replies: 11
    Last Post: 11-01-2009, 01:38 PM
  4. Segmentation Error / Bus Error in ANSI
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-05-2008, 09:45 AM
  5. Segmentation error
    By Kitty Litter in forum C Programming
    Replies: 5
    Last Post: 11-07-2006, 09:35 PM