gpt4 book ai didi

c - 如何编写用指针扫描字符串字符串的程序

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:11 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,要求用户输入几个名称(在本例中为 3,查看我的定义),该程序具有函数 scan_names print_names 会扫描名称并打印它们,无论我做什么我都不会成功:(

这是我得到的异常:“在 Magshimim_EX175.exe 中的 0x0FD6FB7C (ucrtbased.dll) 抛出异常:0xC0000005:读取位置 0x00616161 的访问冲突。”

#include <stdio.h>

#define LINE 3
#define LENGH 10

void print_names(char* names[LENGH], int line)
{
printf("\nYour names are:\n");
for (size_t i = 0; i < line; i++) {
puts(names[i]);
}
}

void scan_names(char* names[LENGH], int line)
{
for (int i = 0; i < line; i++) {
printf("\nEnter name %d: ", i + 1);
fgets(names[i],LENGH,stdin);
}
}

int main(void)
{
char names[LINE][LENGH] = { NULL };
scan_names(names, LINE);
print_names(names, LINE);
}

最佳答案

char names[LINE][LENGH] 是一个二维字符数组。 char* names[LENGH] 是字符指针的一维数组。就像您的编译器告诉您,如果您费心阅读警告/错误:类型不兼容。

将函数更改为 void print_names(char names[LINE][LENGH], int line)

关于c - 如何编写用指针扫描字符串字符串的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062216/

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