gpt4 book ai didi

c - 使用 scanf 修复一些警告

转载 作者:行者123 更新时间:2023-11-30 17:37:48 27 4
gpt4 key购买 nike

int stringCheck(char*);

void compile(int *instructionSet2, int *accumulator2, int *instructionCounter2, int *instructionRegister2, int *operationCode2, int *operand2)
{

char *readIn;
readIn = (char*)malloc(sizeof(char)*11);

while(fgets(readIn,11,stdin) != NULL)
{
*instructionCounter2 = scanf("%d",(int*)readIn);
instructionSet2[*instructionCounter2] = (stringCheck((char*)scanf("%s",readIn)));
instructionSet2[*instructionCounter2] += (scanf("%d",(int*)readIn));
}
}

int stringCheck(char *stIn)
{
if (strcmp(stIn,"READ") == 0)
{
return 10 * 100;
} /* Snipped to just give an idea of the numeric value assignment for words read. */
}

好吧,我在这里想做的是弄清楚如何正确阅读如下一组指令:

00 READ 9
01 READ 10
02 LOAD 9
03 SUB 10
04 BRNG 7
05 WRIT 9
06 HALT 99
07 WRIT 10
08 HALT 99
09 SET 0
10 SET 0

虽然我在尝试阅读文字时遇到了一些困难。基本上,我只想扫描一个 int、一个字符串,然后扫描另一个 int,并在未到达文件末尾时继续执行此操作。

事实证明,scanf 返回成功读取的字符数而不是指针,因此 while 循环内的第二行在编译时警告我这一点。

我正在尝试特别修复此警告:

virtualcomp.c:66:56: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

专门指向这一行:

instructionSet2[*instructionCounter2] = (stringCheck((char*)scanf("%s",readIn)));

我该如何解决这个问题?

注意:我试图避免输入更多变量,仅使用现有的变量。

最佳答案

您可以通过一次 scanf 调用来解析所有三个变量。注意 scanf 返回分配的输入变量的数量。我认为你需要这样的东西:

int n1, n2;
char buf[12];
...
...
sscanf(readIn, "%d %s %d",&n1, buff, &n2);

*instructionCounter2 = n1;
instructionSet2[*instructionCounter2] = stringCheck(buff);
instructionSet2[*instructionCounter2] += n2;

关于c - 使用 scanf 修复一些警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22285823/

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