【发布时间】:2012-09-07 00:13:18
【问题描述】:
我对 C 编程比较陌生,我正在尝试制作一个分隔符检查器,但由于某种原因,每次我运行我的代码并获取我的输入来检查分隔符时,它都有一个 分段错误(核心转储) 错误。
下面是主程序的代码:
int
main (void)
{
char* mystring;
printf ("Please enter a string\n");
gets(mystring);
if (fsm(mystring))
{
printf ("All matched");
}
}
它似乎永远不会进入子程序fsm,因为我在子程序的开头输入了printf,它永远不会出现。如果我将printf 放在子程序行之前的主程序中,它确实会询问我的输入并将其打印出来。
这是我的原型:
boolean fsm(char[]);
如果有帮助的话,这里是子程序的开头:
boolean fsm (char mystring[])
{
printf("here\n");
int counter = -1;
int state = 0;
c_stack top;
c_init_stack (&top);
while (1)
{
switch (state)
{
case 0:
counter = counter + 1;
if (is_open (*mystring))
state = 1;
else if (is_close (*mystring))
state = 2;
else if (mystring = '\0')
state = 3;
else
state = 4;
break;
【问题讨论】:
-
1)
else if (mystring == '\0')(我猜函数参数指向一个字符串字面量?) 2)char* mystring; printf ("Please enter a string\n"); gets(mystring);mystring 指向无处。 3) 不要使用gets()
标签: c segmentation-fault switch-statement fault fsm