无法创建字符串数组。我究竟做错了什么?

时间:2016-07-21 23:40:08

标签: c arrays

我试图编写一个程序,该程序需要一个日期,然后将其转换为一周中的某一天。

我认为我遇到的主要问题是我没有正确创建本周名称的数组,因此,我无法将其传回打印的主要功能。

这是我的代码。如果它很乱,我会提前道歉,我教自己通过书籍编写代码并且避免这么做太久了!

非常感谢任何帮助,非常感谢你:)

#include <stdio.h>

struct DATE
{
    int day;
    int month;
    int year;
};


int main (void)
{
    int  N;

    char day;

    struct DATE date;

    int nConvert(struct DATE date);

    char dayFinder(int N);

    printf("Okay, choose your date:\n");
    scanf("%i:%i:%i", &date.day, &date.month, &date.year); //takes the date and creates a variable withing the DATE struct

    N = nConvert(date); //go convert the date into an integer

    day = dayFinder(N); //go convert N and turn it into a day of the week

    // now we can print out the day of the week that we created in dayFinder
    printf("The day of the week of %i, %i, %i is: %i%i%i\n", date.day, date.month, date.year, day[0], day[1], day[3]);
}


int nConvert(struct DATE N)
{
    int gee(struct DATE); //declare function to get gee and eff(required for conversion equation)
    int eff(struct DATE); 

    int f = eff(N); //call the functions so the outputs can be put into the equation for 'result'
    int g = gee(N);

    int result = (1461 * f) / 4 + (153 * g) / 5 + N.day; //store the result of the equation into a 'result'

    return result; //go put result back into main, will be called 'N'
}


int eff(struct DATE work)
{
    if(work.month <= 2)
    {
        work.year = work.year - 1;
    }
    else
    {
        work.year = work.year;
    }
    return work.year;
}


int gee(struct DATE work)
{
    if(work.month <= 2)
    {
        work.month = work.month + 13;
    }
    else
    {
        work.month = work.month + 1;
    }
    return work.month;
}

char dayFinder(int n)
{
    int convert = (n - 621049) % 7; //convert N sent from main, into integer called converter. 
                                    //we will now have a number that conincides with the day of the week

    char days[7]; //create an array of character strings for each day of the week

    days[0] = {'S', 'u', 'n'};
    days[1] = {'M', 'o', 'n'};
    days[2] = {'T', 'u', 'e'};
    days[3] = {'W', 'e', 'd'};
    days[4] = {'T', 'h', 'u'};
    days[5] = {'F', 'r', 'i'};
    days[6] = {'S', 'a', 't'};

      //now we match the int convert to the right day of the week, then return it back into 'main'
      if(convert == 0)
      {
          return days[0];
      }
      if(convert == 1)
      {
          return days[1];
      }
      if(convert == 2)
      {
          return days[2];
      }
      if(convert == 3)
      {
          return days[3];
      }
      if(convert == 4)
      {
          return days[4];
      }
      if(convert == 5)
      {
          return days[5];
      }
      if(convert == 6)
      {
          return days[6];
      }
}

3 个答案:

答案 0 :(得分:1)

我熟悉这本书:由Stephen Kochan编写的C第4版。

这是一种在不直接使用指针或字符串的情况下完成练习的方法。它使用switch语句来打印文本(字符串)。

/* exercise 8.4 from Programming in C, 4th edition

   the book states in this exercise that August 8, 2004 was a Tuesday;
   actually it was a Sunday    

   exercise 8.2 in the book, on which this exercise is based, also contains
   an error -- there are 198 elapsed days (not 202 days) between August 8,
   2004 and February 22, 2005
*/

#include <stdio.h>

struct date
{
    int day;
    int month;
    int year;
};

int f (int year, int month)
{
    int value;

    if ( month <= 2 )
        value = year - 1;
    else
        value = year;

    return value;
}

int g (int month)
{
    int value;

    if ( month <= 2 )
        value = month + 13;
    else
        value = month + 1;

    return value;
}

int calculateN (struct date d)
{
    return 1461 * f (d.year, d.month) / 4 + 153 * g (d.month) / 5 + d.day;
} 

int main (void)
{
    struct date date1;        

    printf("\nOkay, choose your date (day, month, year): ");
    scanf("%i%i%i", &date1.day, &date1.month, &date1.year);

    printf ("\nday of the week for %i/%i/%i is ", date1.day, date1.month, date1.year);

    switch ((calculateN (date1) - 621049) % 7)
    {
        case 0:
            printf ("Sunday");
            break;
        case 1:
            printf ("Monday");
            break;
        case 2:
            printf ("Tuesday");
            break;
        case 3:
            printf ("Wednesday");
            break;
        case 4:
            printf ("Thursday");
            break;
        case 5:
            printf ("Friday");
            break;
        case 6:
            printf ("Saturday");
            break;
        default:
            printf ("error ");
            break;
    }

    printf ("\n");

    return 0;

}

答案 1 :(得分:0)

您的代码存在3个重要问题,这些问题揭示了解读基本概念时的严重问题

  1. 您对char day[7]的声明不符合您的想法。

    此语言中的char不是字符串,也不是数组。您的char day[7]只是一个7字节的数组,您不能按照您的意愿存储7个字符串

    你需要char *day[7]这是一个由7个指针组成的数组,但是看看你函数的其余部分,绝对不需要数组。

    你可以使用数组来避免使用字符串文字的许多if语句,因为字符串文字存储在内存中允许从函数返回它们的位置,即使它们被声明为函数的局部变量,但通常你会返回地址(或指针)局部变量,因为它们只在你声明它们的函数的堆栈框架内分配,并且当函数返回堆栈时因此,指针在函数调用之前被重置为它的位置,函数本地的所有数据都不再可访问,虽然它可能仍然存在但是没有保证,所以你应该永远不会这样做。 / p>

    做你想做的工作和良好的功能将是

    const char *get_day_name(int week_day)
    {
         const char *days[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
         return days[week_day];
    }
    
  2. 您返回一个字符的值,然后您尝试访问它,就像它是一个数组一样,您的编译器不允许您这样做,因此程序无法确定编译。

    < / LI>
  3. 您的&#34; 字符串&#34;都缺少终止null字节,中的每个字符串必须以null字节'\0'终止,所有期望字符串的函数都需要这个字节告诉字符串的结尾在哪里,如果它不存在然后将它传递给像printf()这样的函数会导致未定义的行为,尽管看起来你打算打印日期字符串的每个字符,在这种情况下,您将不需要终止null字节,您应该习惯字符串需要此字节的事实。它是新程序员使用语言的常见问题来源。

答案 2 :(得分:0)

您的代码中存在许多问题。我清理了其中一些。

#include <stdio.h>

typedef struct
{

    int day;
    int month;
    int year;

} DATE;

int nConvert(DATE N);
int eff(DATE work);
int gee(DATE work);
char* getName(int week_day);

int main (void)
{
    int  N;

    char* day;

    DATE date;

    printf("Okay, choose your day:\n");
    scanf("%i:%i:%i", &date.day,  &date.month, &date.year ); 

    N = nConvert(date); //go convert the date into an integer

    day = getName(N); //go convert N and turn it into a day of the week

    // now we can print out the day of the week that we created in dayFinder
    printf("The day of the week of %i, %i, %i is: %s\n", date.day, date.month, date.year, day);

    return 0;
}


int nConvert(DATE N)
{

    int f = eff(N); //call the functions so the outputs can be put into the equation for 'result'
    int g = gee(N);

    int result = (1461 * f) / 4 + (153 * g) / 5 + N.day; //store the result of the equation into a 'result'

    return result; //go put result back into main, will be called 'N'
}


int eff(DATE work)
{
    if(work.month <= 2)
    {
        work.year = work.year - 1;
    }
    else
    {
        work.year = work.year;
    }
    return work.year;
}


int gee( DATE work)
{
    if(work.month <= 2)
    {
        work.month = work.month + 13;
    }
    else
    {
        work.month = work.month + 1;
    }
    return work.month;
}

char* getName(int day)
{
     const char* days[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
     return days[day%7];
}

如果你真的想要避开*,那么我想你可以这样做:

int main (void)
{
    int  N;

    DATE date;

    printf("Okay, choose your day:\n");
    scanf("%i:%i:%i", &date.day,  &date.month, &date.year ); 

    N = nConvert(date); //go convert the date into an integer

    char names[7][3] = {
        {'S', 'u', 'n'},
        {'M', 'o', 'n'},
        {'T', 'u', 'e'},
        {'W', 'e', 'd'},
        {'T', 'h', 'u'},
        {'F', 'r', 'i'},
        {'S', 'a', 't'}
    };
    int dayNum = N%7;

    // now we can print out the day of the week that we created in dayFinder
    printf("The day of the week of %i, %i, %i is: %c%c%c\n", date.day, date.month, date.year, names[dayNum][0], names[dayNum][1], names[dayNum][2]);

    return 0;
}

但是你仍在使用指针而你却无法避免它们。