3

day_of_year练习(5-9):用指针而不是索引重写例程。

static char daytab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

/* day_of_year: set day of year from month and day */
int day_of_year(int year, int month, int day)
{
    int i, leap;

    leap = (year%4 == 0) && (year%100 != 0) || (year%400 == 0);
    for (i = 1; i < month; i++)
    {
        day += daytab[leap][i];
    }

    return day;
}

我可能只是累了,没有思考,但实际上如何创建一个带有指针的多维数组?

我可能会弄清楚该函数的其余部分,但我无法正确使用语法。

4

4 回答 4

2

有两种处理方法:

第一个是模仿 C 实际处理多维数组的方式,也就是说完全没有。char[4][4] 实际上只是 char[16] 周围的语法糖。您可以创建一个指向 16 字节数组的指针(在本例中),并且得到了相同的结果。

另一种是创建指向指针的指针。按照前面的例子:

char **foo = malloc(sizeof(char *) * 4);
for(int i = 0; i < 4; ++i)
    foo[i] = malloc(sizeof(char) * 4);
foo[0][0] = bar;
于 2009-01-28T00:20:44.697 回答
2

The following complete program will do what you desire. I've converted your array into a char pointer (string) and interspersed the leap year values. I've also removed the dummy entries and adjusted the loop.

The test program lacks serious error checking so don't expect it to work with dodgy arguments.

#include <stdio.h>

static char *daytab =
    "\x1f\x1f\x1c\x1d\x1f\x1f"
    "\x1e\x1e\x1f\x1f\x1e\x1e"
    "\x1f\x1f\x1f\x1f\x1e\x1e"
    "\x1f\x1f\x1e\x1e\x1f\x1f";

/* day_of_year: set day of year from month and day */
int day_of_year(int year, int month, int day) {
    int i, leap;

    leap = (year%4 == 0) && (year%100 != 0) || (year%400 == 0);
    for (i = 0; i < month-1; i++) {
        day += *(daytab+i*2+leap);
    }

    return day;
}

int main (int argc, char *argv[]) {
    if (argc != 4) {
        printf ("Usage: blah yy mm dd\n");
        return 1;
    }
    printf ("%4.4s/%2.2s/%2.2s -> %04d/%02d/%02d -> %d\n",
        argv[1], argv[2], argv[3],
        atoi (argv[1]), atoi (argv[2]), atoi (argv[3]),
        day_of_year (atoi(argv[1]),atoi(argv[2]),atoi(argv[3])));
    return 0;
}
于 2009-01-28T01:57:24.907 回答
2

You're just asked to modify the day_of_year routine, not the daytab declaration. I would leave that array as-is, and modify day_of_year as follows:

/* day_of_year: set day of year from month and day */
int day_of_year(int year, int month, int day)
{
    char* p = (year%4 == 0) && (year%100 != 0) || (year%400 == 0) ? 
        daytab[0] : daytab[1];

    p++;
    for (i = 1; i < month; i++, p++)
    {
        day += *p;
    }

    return day;
}

If you want to the declaration of p to be shorter, you can do this:

    char* p = daytab[(year%4 == 0) && (year%100 != 0) || (year%400 == 0)];

If you still want to remove that access, too:

    char* p = *(daytab + ((year%4 == 0) && (year%100 != 0) || (year%400 == 0)));

One might argue that it looks ugly, but hey, that's what you get with pointers.

于 2009-01-28T02:33:46.003 回答
0

Old thread, but I actually have a solution that doesn't use indexing, not even [0]. So instead of writing

day += daytab[leap][i];

write

day += *(*(daytab+leap)+i);
于 2015-07-05T14:28:51.940 回答