gokusora34
gokusora34

Reputation: 11

Why do I get this segmentation fault?

I'm working on a my pre-lab in which I'm supposed to take pre-defined strings and print if they're all uppercase, lowercase, have digits, or a mixture. However, I keep getting a segmentation fault(core dumped). I tried debugging, but I have no clue what the problem is. Any help is appreciated!

This is my main.c

#define DIGITTEST1 "12345abcd67890"
#define DIGITTEST2 "1234567890"
#define LOWERTEST1 "hifromJimRies"
#define LOWERTEST2 "hifromjimries"
#define UPPERTEST1 "JIMRIESWASHERE"
#define UPPERTEST2 "JIMRIESWASHERe"
#define ONEMORETEST "9jimr"

void PrintCategory(char *s);

int main(void)
{
PrintCategory(DIGITTEST1);
PrintCategory(DIGITTEST2);
PrintCategory(LOWERTEST1);
PrintCategory(LOWERTEST2);
PrintCategory(UPPERTEST1);
PrintCategory(UPPERTEST2);
PrintCategory(ONEMORETEST);
}

This is my stringfunc.c

 #include<stdio.h>
    #include<ctype.h>
    #define DIGITTEST1 "12345abcd67890"
    #define DIGITTEST2 "1234567890"
    #define LOWERTEST1 "hifromJimRies"
    #define LOWERTEST2 "hifromjimries"
    #define UPPERTEST1 "JIMRIESWASHERE"
    #define UPPERTEST2 "JIMRIESWASHERe"
    #define ONEMORETEST "9jimr"

void PrintCategory(char *s)
{
  if (isdigit (DIGITTEST1))
    {
      printf ("%s is all digits\n", DIGITTEST1);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", DIGITTEST1);
    }

  if (isdigit (DIGITTEST2))
    {
      printf ("%s is all digits\n", DIGITTEST2);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", DIGITTEST2);
    }
  if (islower (LOWERTEST1))
    {
      printf ("%s is all lower case\n", LOWERTEST1);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", LOWERTEST1);
    }
  if (islower (LOWERTEST2))
    {
      printf ("%s is all lower case\n", LOWERTEST2);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", LOWERTEST2);
    }
  if (isupper (UPPERTEST1))
    {
      printf ("%s is all upper casae\n", UPPERTEST1);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", UPPERTEST1);
    }
  if (isupper (UPPERTEST2))
    {
      printf ("%s is all upper case\n", UPPERTEST2);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", UPPERTEST2);
    }
  if (islower (ONEMORETEST))
    {
      printf ("%s is all lower case\n", ONEMORETEST);
    }
  else
    {
      printf ("%s is a mix of various types of characters\n", ONEMORETEST);
    }

}

Upvotes: 0

Views: 72

Answers (2)

wovano
wovano

Reputation: 5093

The functions from ctype.h (e.g. isdigit, islower, etc.) are working on characters. Their argument is of type int. However, you pass a string (type char *) to it. This does compile, because a char * can be implicitly converted to int. However, in this case the code is incorrect and results in undefined behavior (in this case a segmentation fault).

The compiler warns you about this error. For example, when I compile your code I get the following warnings:

stringfunc.c: In function ‘PrintCategory’:
stringfunc.c:13:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
stringfunc.c:22:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
stringfunc.c:30:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
stringfunc.c:38:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
stringfunc.c:46:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
stringfunc.c:54:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
stringfunc.c:62:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]

I highly recommend to compile with compiler options -Wall and -Werror if using gcc (other compiler might have similar options) to avoid these kind of programming errors. Using -Werror this code would not have compiled.

Upvotes: 2

D.Malim
D.Malim

Reputation: 116

Your passing character array to isdigit function as shown below here.

Program received signal SIGSEGV, Segmentation fault.
0x00000000004005cf in PrintCategory (s=0x400880 "12345abcd67890") at String.c:13
13    if (isdigit (DIGITTEST1))
Missing separate debuginfos, use: debuginfo-install glibc-2.17-292.el7.x86_64

Upvotes: -1

Related Questions