gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:40 25 4
gpt4 key购买 nike

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

#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 是空的。我认为问题出在使用 scanf 上,但正如我所说,其他功能也无法正常工作。我希望任何人都可以帮助我。谢谢

编辑:我通过strlen(eaf)检查

最佳答案

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

最终,字符串为空,因为您的数组维度有误。您为“initials”指定了一个大小为 1 的数组,它没有为尾随的 '\0' C 字符串终止符提供空间。

ideone 上查看此代码的现场演示:

#include <stdio.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(char eaf[], char initials[], char foo[])
{
printf("load\n");
report(eaf, initials, foo);

printf("Enter EAF\n");
scanf(" %s", eaf);
report(eaf, initials, foo);

printf("Enter initial state\n");
scanf(" %s", initials);
report(eaf, initials, foo);
}

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

return 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;
}

观看现场演示 ideone here .

关于c - 为什么这个字符串是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com