gpt4 book ai didi

c - Fscanf 段错误

转载 作者:行者123 更新时间:2023-11-30 19:20:40 25 4
gpt4 key购买 nike

我在尝试打印字典的前 6 个单词时一直遇到段错误。我很确定我错误地使用了 fscanf,但我不确定如何/为什么......

#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>

int main(int argc, char* enc[])
{
if (argc != 2)
{
printf("Improper command-line arguments\n");
return 1;
}

FILE *Dict;
Dict = fopen("/usr/share/dict/words", "r");

if (Dict == NULL)
{
printf("Could not open dictionary");
exit(1);
}

char* full = enc[1];
char* salt[2];

for (int i=0; i<2; i++)
{
salt[i] = &full[i];
}

char* key[50];

for (int i=0; i<6; i++)
{
fscanf(Dict, "%s", *key);
printf("%s", *key);
}
}

最佳答案

C 字符串可以是字符数组:char name[10],也可以是指向字符的指针(指向有效的内存范围):char* name >.

这里有一个包含 50 个字符(或字符串)指针的数组:

char* key[50];

for (int i=0; i<6; i++)
{
fscanf(Dict, "%s", *key);
printf("%s", *key);
}

key 可能是一个 50 字符的 C 字符串缓冲区:

char key[50];

for (int i=0; i<6; i++)
{
fscanf(Dict, "%s", key);
printf("%s", key);
}

关于c - Fscanf 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21744078/

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