我一直在尝试将一个程序从 Java 转换为 C,它是 watch 的模拟器并显示我正在使用 Ascii 艺术的时间。我已将所有数字 (0-9) 存储在二维字符数组 (fx.9) 中:
char nine[7][5] = {
{ '0', '0', '0', '0' },
{ '0', ' ', ' ', '0' },
{ '0', ' ', ' ', '0' },
{ '0', '0', '0', '0' },
{ ' ', ' ', ' ', '0' },
{ ' ', ' ', ' ', '0' },
{ ' ', ' ', ' ', '0' }
};
我现在有一个函数,它的工作是转换存储在一个 int 数组中的时间(fx.22:04:59,将在数组中存储为 220459)。该函数应将相应的 Ascii 艺术返回给每个数字,以便我最终可以调用打印时间的函数(以 ascii 形式),该函数采用 6 个 char[][] 参数。
所以简而言之,我需要知道用哪 6 个参数来调用这个函数:
void printScreen(char hourFirstDigit[7][5], char hourSecondDigit[7][5], char minuteFirstDigit[7][5], char minuteSecondDigit[7][5], char secFirstDigit[7][5], char secSecondDigit[7][5])
在 Java 中,我的解决方案是简单地创建一个返回 char[][] 数组的函数,然后是 10 种情况 (0-9) 的 switch 语句,(这里是前几行):
char timeToAsciiArt[][](int digitNumber, int small) {
switch (timeRightNow[digitNumber]) {
case 0:
return zero;
}
可能的解决方案: 我读过那里至少有两种可能的问题解决方案(通常),1. 用指向数组的指针替换。 2. 用结构包裹。
我的看法: 1. 我真的不确定如何返回指向数组的指针(有人可以解释如何以 case 0: 为例吗?:)
最佳答案
据我了解,您希望:
- 您的 ASCII 艺术作品的某种存储方式;
- 接收数字并返回指向相应 ASCII 艺术存储的指针的函数;
- 接收一组 ASCII 艺术并打印它们的函数。
存储ASCII艺术
将您的 ASCII 艺术包装到一个结构中可能是明智的,因为您可以随后存储更多关于它的数据。或许将来你会想要更细的 1
数字;您需要存储有关每个 ASCII 艺术作品大小的数据:
struct ascii_digit {
unsigned int width;
unsigned int height;
char[MAX_HEIGHT][MAX_WIDTH] art; //Here you could instead use a pointer
//instead of storing directly
}
当然,如果您绝对不打算使用固定大小以外的其他东西,数组也可以。
寻找正确的 ASCII 艺术
在 C 中传递数组时,您通常不会直接传递数组:您通常使用指向它的指针。所以你的函数的正确原型(prototype)不会是
char time_to_ascii_art[][](int digit_number, int small);
而是,如果你使用结构
struct ascii_digit* time_to_ascii_art(int digit_number, int small);
Note that we return a pointer to a structure. While it is absolutely possible to directly pass a structure, it may not be considered good practice as it induces some overhead depending on the size of your structure.
或者您可以使用指向 char
的指针的简单方法:
char* time_to_ascii_art(int digit_number, int small);
请注意,如果您有一个指向二维数组的 char*
指针,则在尝试访问其内容时您将不得不自己进行数学计算。例如,访问第 x
行的第 y
成员:array[width * x + y]
。为了避免这样做,您可以使用指向数组的指针:
char (*time_to_ascii_art)(int digit_number, int small)[ASCII_ART_WIDTH];
在此函数中,您可以像在 Java 中那样使用 switch … case
语句(例如使用结构):
// Let's say that your structures are declared in the global scope as ascii_zero, ascii_one…
struct ascii_digit* time_to_ascii_art(int digit_number, int small)
{
switch(digit_number) {
case 0:
return ascii_zero;
case 1:
return ascii_one;
default:
return NULL;
}
}
或者你可以——这在 Java 中也可能是个好主意——有一个包含你的 ASCII 艺术的数组,或者指向它们的指针,索引以便访问第 n
成员会给你数字 n
的 ASCII 艺术:
// Let's now say you have an ascii_digits array of structs containing your digits
struct ascii_digit* time_to_ascii_art(int digit_number, int small)
{
return ascii_digits + digit_number; // You should handle bad values.
// ascii_digits being an array, it is implicitly cast to a pointer here.
}
将 ASCII 艺术传递给显示函数
现在要将您的 ASCII 艺术传递给您的显示函数,您可以——根据您选择的数据类型——使用指向结构的指针:
void print_screen(struct ascii_digit* hour_first_digit, …);
指向 char
的指针:
void print_screen(char* hour_first_digit, …);
或指向字符数组的指针:
void print_screen(char (*hour_first_digit)[ASCII_ART_WIDTH], …);
关于c - C 新手,返回指向二维数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33543781/