C 为什么这个字符串是空的?

C 为什么这个字符串是空的?,c,string,function,C,String,Function,我有个小问题,解决不了。 问题是,我试图通过请求用户插入来加载函数load中的所有四个字符串。一切似乎都很好,我没有得到任何编译器错误。但是eaf保持为空。我尝试了很多方法,甚至用gets,gets,fgets替换scanf,但没有任何改变 #include <conio.h> #include <stdio.h> #include <string.h> void LOAD(char eaf[], char initials[], char finals[]

我有个小问题,解决不了。
问题是,我试图通过请求用户插入来加载函数
load
中的所有四个字符串。一切似乎都很好,我没有得到任何编译器错误。但是
eaf
保持为空。我尝试了很多方法,甚至用
gets
gets
fgets
替换
scanf
,但没有任何改变

#include <conio.h>
#include <stdio.h>
#include <string.h>

void LOAD(char eaf[], char initials[], char finals[], char symbols[]);
int COMPARE(char s[], char eaf[]);

int main()
{
    char eaf[10],initials[1],finals[10],symbols[5];
    LOAD(eaf, initials, finals, symbols);
    return 0;
}


void LOAD(char eaf[], char initials[], char finals[], char symbols[])
{
    printf("Insert states of the optimized AFD\n");
    scanf( " %s", eaf);

    printf("Insert AFD initial state\n");
    do
    {
        scanf( " %s", initials);
    } while (COMPARE(initials, eaf));

    printf("Insert final state(s)\n");
    do
    {
        scanf( " %s",finals);
    } while (COMPARE(finals, eaf));

    printf("Insert the language symbols\n");
    scanf( " %s",symbols);
}

int COMPARE(char s[], char eaf[])
{
    int i;
    char *ptr;
    for(i; i < strlen(s); i++){
            printf("%d\n", i);
        while(ptr==NULL){
            ptr = strchr(eaf, *s);
        }
    }
    if (ptr == NULL) return 1;
    else return 0;
}
#包括
#包括
#包括
无效载荷(字符eaf[],字符首字母[],字符韵母[],字符符号[]);
int比较(字符s[],字符eaf[]);
int main()
{
字符eaf[10],首字母[1],韵母[10],符号[5];
荷载(eaf、首字母、韵母、符号);
返回0;
}
无效加载(字符eaf[],字符首字母[],字符韵母[],字符符号[])
{
printf(“插入优化AFD的状态”);
scanf(“%s”,电弧炉);
printf(“插入AFD初始状态\n”);
做
{
scanf(“%s”,缩写);
}而(比较(首字母缩写,eaf));
printf(“插入最终状态)\n”);
做
{
scanf(“%s”,总决赛);
}而(比较(期末考试,eaf));
printf(“插入语言符号”);
scanf(“%s”,符号);
}
int比较(字符s[],字符eaf[]
{
int i;
char*ptr;
对于(i;i
我做错了什么?这只是一个更大程序的一小部分,但其余部分是无用的,因为
eaf
是空的。我认为问题在于使用scanf
scanf
,但正如我所说的,其他函数也不起作用。我希望任何人都能帮助我。谢谢


编辑:我通过
strlen(eaf)

检查了
scanf
中的格式字符串很可能是罪魁祸首;在
%s
之前有一个额外的空间

更改:

scanf( " %s", eaf);

对于所有的
scanf

它不会将您的输入放入
eaf
,因为它正在查找格式为“blahblahblah(注意开头的空格)而不是“blahblahblah”的字符串

编辑 无视上述,

空白:任何空白字符都会触发对零个或多个空白字符的扫描。空白字符的数量和类型不需要在两个方向上都匹配


另外,您应该在
COMPARE
函数中初始化
i
(我不明白您为什么没有收到编译器发出的愤怒警告),我没有做任何修改就运行了您的代码,并且
strlen(eaf)
返回了正确的计数(就在
scanf
之后).

您在
scanf中的格式字符串很可能是罪魁祸首;在
%s
之前有一个额外的空间

更改:

scanf( " %s", eaf);

对于所有的
scanf

它不会将您的输入放入
eaf
,因为它正在查找格式为“blahblahblah(注意开头的空格)而不是“blahblahblah”的字符串

编辑 无视上述,

空白:任何空白字符都会触发对零个或多个空白字符的扫描。空白字符的数量和类型不需要在两个方向上都匹配

另外,您应该在
COMPARE
函数中初始化
i
(我不明白您为什么没有收到编译器发出的愤怒警告),我没有做任何修改就运行了您的代码,并且
strlen(eaf)
返回了正确的计数(就在
scanf
之后)。

使用“scanf”输入是危险的,而你已经走进了危险之中。当您要求它以字符串形式读取首字母时,您允许它覆盖“eaf”的内容,并添加终止0

最后,字符串是空的,因为您的数组维度错误。您为“initials”指定的数组大小为1,它不为结尾的“\0”C字符串终止符提供空间

请参见以下网站上的此代码的实时演示:

#包括
无效报告(char*eaf,char*initials,char*foo)
{
printf(“eaf=%p,缩写=%p,foo=%p\n”,eaf,缩写,foo);;
printf(“*eaf=%d,*首字母=%d,*foo=%d\n”,eaf[0],首字母[0],foo[0]);
}
无效负载(字符eaf[],字符首字母[],字符foo[]
{
printf(“加载\n”);
报告(eaf,缩写,foo);
printf(“输入EAF\n”);
scanf(“%s”,电弧炉);
报告(eaf,缩写,foo);
printf(“进入初始状态\n”);
scanf(“%s”,缩写);
报告(eaf,缩写,foo);
}
int main(int argc,const char*argv[]
{
字符eaf[10],首字母[1],foo[10];
报告(eaf,缩写,foo);
负荷(电弧炉,首字母缩写,foo);
报告(eaf,缩写,foo);
返回0;
}
您应该在调试器中遍历此过程,并观察“eaf”和“initials”的值,以查看在进行过程中发生了什么

你必须用C写这个程序吗?似乎使用诸如perl或python之类的脚本语言对您来说可能更容易

这里有一个有效的C方法来开始解决这个问题,请注意,我实际上并没有解决这个问题,但它会使问题更容易被发现

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

void report(char* eaf, char* initials, char* foo)
{
    printf("eaf = %p, initials = %p, foo = %p\n", eaf, initials, foo);;
    printf("*eaf = %d, *initials = %d, *foo = %d\n", eaf[0], initials[0], foo[0]);
}

void load(const char* label, const char* into, size_t intoSize)
{
    assert(intoSize > 1); // can't store a string in 1 character.
    printf("%s\n", label);

    char input[1024] = "";
    fgets(input, sizeof(input), stdin);

    size_t len = strlen(input);
    // strip trailing \n off.
    if (len > 0 && input[len - 1] == '\n') {
        input[--len] = 0;
    }

    // abort on empty input
    if (len <= 0) {
        fprintf(stderr, "Invalid input - terminated.\n");
        exit(1);
    }

    if (len >= intoSize) {
        fprintf(stderr, "Invalid input - length was %u, limit is %u\n", len, intoSize - 1);
        exit(2);
    }

    strncpy(into, input, intoSize);
}

int main(int argc, const char* argv[])
{
    char eaf[10], initials[1], foo[10];
    report(eaf, initials, foo);

    load("Insert states of the optimized AFD", eaf, sizeof(eaf));
    report(eaf, initials, foo);

    load("Insert initial AFD state", initials, sizeof(initials));
    report(eaf, initials, foo);

    printf("eaf = %s\ninitials = %s\n", eaf, initials);

    return 0;
}
#包括
#include.

使用“scanf”输入是危险的,您已经直接进入了这种危险。当您要求它以字符串形式读取首字母时,您允许它覆盖“eaf”的内容,并添加终止0

最后,字符串是空的,因为您的数组维度错误。您为“initials”指定的数组大小为1,它不为结尾的“\0”C字符串终止符提供空间

请参见以下网站上的此代码的实时演示:

#包括
无效报告(char*eaf,char*initials,char*foo)
{
printf(“eaf=%p,缩写=%p,foo=%p\n”,eaf,缩写,foo);;
printf(“*eaf=%d,*首字母=%d,*foo=%d\n”,eaf[0],首字母[0],foo[0]);
}
空荷载(char)
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

void report(char* eaf, char* initials, char* foo)
{
    printf("eaf = %p, initials = %p, foo = %p\n", eaf, initials, foo);;
    printf("*eaf = %d, *initials = %d, *foo = %d\n", eaf[0], initials[0], foo[0]);
}

void load(const char* label, const char* into, size_t intoSize)
{
    assert(intoSize > 1); // can't store a string in 1 character.
    printf("%s\n", label);

    char input[1024] = "";
    fgets(input, sizeof(input), stdin);

    size_t len = strlen(input);
    // strip trailing \n off.
    if (len > 0 && input[len - 1] == '\n') {
        input[--len] = 0;
    }

    // abort on empty input
    if (len <= 0) {
        fprintf(stderr, "Invalid input - terminated.\n");
        exit(1);
    }

    if (len >= intoSize) {
        fprintf(stderr, "Invalid input - length was %u, limit is %u\n", len, intoSize - 1);
        exit(2);
    }

    strncpy(into, input, intoSize);
}

int main(int argc, const char* argv[])
{
    char eaf[10], initials[1], foo[10];
    report(eaf, initials, foo);

    load("Insert states of the optimized AFD", eaf, sizeof(eaf));
    report(eaf, initials, foo);

    load("Insert initial AFD state", initials, sizeof(initials));
    report(eaf, initials, foo);

    printf("eaf = %s\ninitials = %s\n", eaf, initials);

    return 0;
}