-7

My code is repeatedly giving me the error message segmentation fault even though I have no idea what it means. I am trying to create a Caesar cipher but my code keeps giving me error messages. I have no idea what the problem is and any help will be appreciated.

#include <stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
void encrypt(int k,string text);
int main(int argc,string arcv[])
{
    if(argc != 2)
    {         
         printf ("Please return a vaild command line argument");
         return 1;
    }
    else
    {
    printf("Enter your text here:");
    string s = GetString();
    int x;  
    x = (int) (arcv[1] - '0');
    encrypt(x,s);
    }
}


void encrypt(int k,string text)
{  
  if(k > 26)
  {
      k = k % 26;
  }
  for(int i = 0;i < strlen(text); i++)  
  { 
  if(isalpha(text))
  {
      printf("%c",text[i] + k);
  }
  else
  {
      printf("%c",text[i]);
  }
  }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zubin JAIN
  • 49
  • 3
  • what is gdb? If you mean compiler ? I am using clang – Zubin JAIN Aug 24 '15 at 12:41
  • 2
    1) `x = (int) (arcv[1] - '0');` --> `x = (int) (*arcv[1] - '0');` 2) `if(isalpha(text))` --> `if(isalpha(text[i]))` – BLUEPIXY Aug 24 '15 at 12:49
  • **This is not C!** What is `string`? Please provide a [mcve]. – too honest for this site Aug 24 '15 at 13:01
  • 1
    gdb is the gnu debugger. in your case can use lldb. the point is it will say exactly where it segfaults. give lldb a google and read some tutes on using it – amdixon Aug 24 '15 at 13:02
  • 1
    @Olaf It is speculated that it is `typedef char* string` – BLUEPIXY Aug 24 '15 at 13:03
  • @ameyCU: Thanks. But it is still not **complete**. – too honest for this site Aug 24 '15 at 13:04
  • @Olaf yes but he cant just copy paste the whole implementated header here. – ameyCU Aug 24 '15 at 13:04
  • Doesn't clang produce some warnings when compiling your code? Don't ignore them and pay attention to them. And the code you've posted seems incomplete. – Spikatrix Aug 24 '15 at 13:06
  • The problem resides where @BLUEPIXY pointed out . He should correct that. – ameyCU Aug 24 '15 at 13:08
  • I have and it works now – Zubin JAIN Aug 24 '15 at 13:10
  • And the string comes from the cs50 library I am using – Zubin JAIN Aug 24 '15 at 13:10
  • ...... because you have not debugged it. – Martin James Aug 24 '15 at 14:02
  • regarding this line: ''printf ("Please return a vaild command line argument");' This should be a 'usage' statement, something like: 'printf( "USAGE: %s \n", argv[0] );' Note: argv[1] is a char * to the first command line parameter. so argv[1] - '0' is subtracting '0' from an address (BTW: '0' is 0x30). suggest assure the character at: argv[0][0] is actually a number, by: 'if( isdigit( argv[0][0] )' before subracting '0' to convert the ascii digit to a number in the range 0...9 – user3629249 Aug 24 '15 at 20:39
  • @Olaf, the 'string' is defined in the cs50.h file as 'char *' (I personally think the cs50.h file should never be included as it causes the student to use functions and type names that are not going to be available anywhere else.) I.E. it is not portable – user3629249 Aug 24 '15 at 20:41
  • regarding this line: 'if(k > 26)' 'k' can never by greater than 26 because the first character in the command line parameter is expected to be 0...9 – user3629249 Aug 24 '15 at 20:44
  • the code has a significant logic error. It is printing the ascii character set for characters a...z and A...Z which are offset by the value in 'k'. For many of the ascii character set that will pass the 'isalpha()' macro, the resulting character is not printable. so printing the resulting character is an error. BTW: isalpha() works on a single character, not a pointer to an array of characters – user3629249 Aug 24 '15 at 20:52
  • @user3629249: Thanks, I already assumed something like that. Which idi** obfuscates a pointer like that - even worse to a standard type? No wonder students get confused. – too honest for this site Aug 24 '15 at 22:30

1 Answers1

3

You should probably have a look at https://en.wikipedia.org/wiki/Segmentation_fault.

Basically it means that your code is trying to use data at an address in an invalid way. A common case in C code is something like this:

char * cptr = NULL ;
cptr = some_function_returning_null();
printf("%s\n"cptr);

The memory at pointer cptr has never been set properly. The attempt to print the string will cause a seg fault.

In your case I think isalpha(text) should be isalpha(text[i]), but I doubt this is causing the problem.

Perhaps if you supplied the full trace, including your input arguments, it would be easier to spot the problem.

lrnop
  • 66
  • 1