Как использовать функцию scanf для массива - PullRequest
0 голосов
/ 11 декабря 2019

Я хочу найти дату, месяц и год по заданному набору чисел, используя массив и указатели. Я пишу программу для изучения 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;
}

Также я не инициализировалразмер массива, но он продолжал вызывать ошибку при объявлении массива.

Ответы [ 2 ]

0 голосов
/ 11 декабря 2019

Вы, вероятно, хотите это:

#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;
}

или даже проще:

#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;
}

Пример:

Введите год, который вы хотите
01022019
Сегодня день 01, месяц 02, а год 2019

Но имейте в виду, что это очень неудобно, потому что вы вообще не проверяете,ввод правдоподобен, попробуйте ввести abcdefg вместо 01022019 и посмотреть, что произойдет.

0 голосов
/ 11 декабря 2019

Ваш сканф должен был быть таким 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...