gpt4 book ai didi

c - 如何对数组使用 scanf 函数

转载 作者:行者123 更新时间:2023-11-30 16:08:39 25 4
gpt4 key购买 nike

我想使用数组和指针通过给定的一组数字查找日期、月份和年份。我正在编写程序来学习 C。

下面是输出:

输入号码:22102013今天是 22 号,月份是 10 号,年份是 2013 年。

我编写了以下代码。

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

int main()
{
char date [11];

char days []="DD";
char month[]="MM";
char year []="YYYY";

printf("Enter the year that you want \n");
scanf ("%d",&date[11]);


char *my_pointer = date;
days[0] = *(my_pointer);
days[1] = *(my_pointer+1);


month[0]=*(my_pointer+2);
month[1]=*(my_pointer+3);

year [0]=*(my_pointer+4);
year [1]=*(my_pointer+5);
year [2]=*(my_pointer+6);
year [3]=*(my_pointer+7);

printf("Today the day is %s,the month is %s and the year is %s",days,month,year);
return 0;
}

此外,我还没有初始化数组大小,但它不断引发数组声明错误。

最佳答案

你的 scanf 应该像这样 scanf ("%s",date);

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

int main()
{
char date [11];

char days []="DD";
char month[]="MM";
char year []="YYYY";

printf("Enter the year that you want \n");
scanf ("%s",date);


char *my_pointer = date;
days[0] = *(my_pointer);
days[1] = *(my_pointer+1);


month[0]=*(my_pointer+2);
month[1]=*(my_pointer+3);

year [0]=*(my_pointer+4);
year [1]=*(my_pointer+5);
year [2]=*(my_pointer+6);
year [3]=*(my_pointer+7);

printf("Today the day is %s,the month is %s and the year is %s",days,month,year);
return 0;
}

关于c - 如何对数组使用 scanf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290442/

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