如何对阵列使用scanf函数

如何对阵列使用scanf函数,c,arrays,pointers,C,Arrays,Pointers,我想通过一组给定的数字,使用数组和指针来查找日期、月份和年份 以下是输出: 输入号码: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("

我想通过一组给定的数字,使用数组和指针来查找日期、月份和年份

以下是输出:

输入号码: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;
}
#包括
#包括
int main()
{
字符日期[11];
字符天数[]=“DD”;
字符月份[]=“MM”;
字符年份[]=“YYYY”;
printf(“输入您想要的年份\n”);
scanf(“%d”和日期[11]);
char*my_指针=日期;
天[0]=*(我的_指针);
天[1]=*(我的_指针+1);
月[0]=*(我的_指针+2);
月[1]=*(我的_指针+3);
年份[0]=*(我的_指针+4);
年份[1]=*(我的_指针+5);
年份[2]=*(我的_指针+6);
年份[3]=*(我的_指针+7);
printf(“今天是%s,月份是%s,年份是%s”,天,月,年);
返回0;
}

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

您的scanf应该是这样的
scanf(“%s”,date)

#包括
#包括
int main()
{
字符日期[11];
字符天数[]=“DD”;
字符月份[]=“MM”;
字符年份[]=“YYYY”;
printf(“输入您想要的年份\n”);
scanf(“%s”,日期);
char*my_指针=日期;
天[0]=*(我的_指针);
天[1]=*(我的_指针+1);
月[0]=*(我的_指针+2);
月[1]=*(我的_指针+3);
年份[0]=*(我的_指针+4);
年份[1]=*(我的_指针+5);
年份[2]=*(我的_指针+6);
年份[3]=*(我的_指针+7);
printf(“今天是%s,月份是%s,年份是%s”,天,月,年);
返回0;
}

您可能想要:

#include <stdio.h>

int main()
{
  char date[11];

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

  printf("Enter the year that you want \n");
  scanf("%10s", date);  // limit input to 10 chars to prevent buffer overflow

  days[0] = date[0];
  days[1] = date[1];

  month[0] = date[2];
  month[1] = date[3];

  year[0] = date[4];
  year[1] = date[5];
  year[2] = date[6];
  year[3] = date[7];

  printf("Today the day is %s, the month is %s and the year is %s", days, month, year);
  return 0;
}
#包括
int main()
{
字符日期[11];
字符天数[]=“DD”;
字符月份[]=“MM”;
字符年份[]=“YYYY”;
printf(“输入您想要的年份\n”);
scanf(“%10s”,date);//将输入限制为10个字符以防止缓冲区溢出
天[0]=日期[0];
天[1]=日期[1];
月[0]=日期[2];
月[1]=日期[3];
年份[0]=日期[4];
年份[1]=日期[5];
年份[2]=日期[6];
年份[3]=日期[7];
printf(“今天是%s,月份是%s,年份是%s”,天,月,年);
返回0;
}
或者更简单:

#include <stdio.h>
#include <string.h>

int main()
{
  char date[11];

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

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

  memcpy(days,  date + 0, 2);
  memcpy(month, date + 2, 2);
  memcpy(year,  date + 4, 4);

  printf("Today the day is %s, the month is %s and the year is %s", days, month, year);
  return 0;
}
#包括
#包括
int main()
{
字符日期[11];
字符天数[]=“DD”;
字符月份[]=“MM”;
字符年份[]=“YYYY”;
printf(“输入您想要的年份\n”);
scanf(“%10s”,日期);
memcpy(天,日期+0,2);
memcpy(月、日+2,2);
memcpy(年份、日期+4,4);
printf(“今天是%s,月份是%s,年份是%s”,天,月,年);
返回0;
}
例如:

输入您想要的年份
01022019
今天是01,月份是02,年份是2019


但请注意,这是非常不方便的,因为您根本不检查输入是否合理,请尝试输入
abcdefg
,而不是
01022019
,然后看看会发生什么情况。

scanf(“%d”,&date[11])->
scanf(“%d”,日期)而不是
days[0]=*(我的指针)
等。您可以简单地编写
days[0]=date[0]
days[1]=date[1]
days[2]=date[2]
等等。我假设你真的想要像
scanf(“%10s”,date)
“但它一直在引发错误”:当你做什么时谁会引发哪个错误?编译器一直在引发错误:“date”中缺少数组大小
#include <stdio.h>
#include <string.h>

int main()
{
  char date[11];

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

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

  memcpy(days,  date + 0, 2);
  memcpy(month, date + 2, 2);
  memcpy(year,  date + 4, 4);

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