我正在做一个预实验,在其中应该采用预定义的字符串并打印它们是否全部为大写,小写,有数字或混合字符。但是,我不断遇到分段错误(核心已转储)。我尝试调试,但不知道问题出在哪里。任何帮助表示赞赏!
这是我的主力。
#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);
}
这是我的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);
}
}
答案 0 :(得分:2)
ctype.h中的函数(例如isdigit
,islower
等)正在处理字符。他们的论点是int
类型。但是,您将字符串(类型char *
)传递给它。确实可以编译,因为可以将char *
隐式转换为int
。但是,在这种情况下,代码不正确并导致未定义的行为(在这种情况下为分段错误)。
编译器会警告您有关此错误。例如,当我编译您的代码时,会收到以下警告:
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]
如果使用gcc(其他编译器可能具有类似的选项),我强烈建议使用编译器选项-Wall
和-Werror
进行编译,以避免此类编程错误。使用-Werror
,此代码将无法编译。
答案 1 :(得分:0)
将字符数组传递给isdigit函数,如下所示。
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