尽管最简单的更改是将符号从 更改matrix[i][j]为*(*(matrix + i) + j),但您可以通过注意数组的所有元素都是连续的,从而使用更少的函数来编写更精简的代码,因此您可以简单地使用指针迭代数组的元素,而永远不会添加偏移量。
这导致这样的代码:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define SIZE 4
#define ROWS 3
#define COLS 20
void matRandomChar(char *mat, int rows, int cols);
void printMat(const char *tag, char *base, int rows, int cols);
void matRandomChar(char *mat, int rows, int cols)
{
char *end = mat + rows * cols;
while (mat < end)
*mat++ = 'A' + rand() % ('Z' - 'A' + 1);
}
void printMat(const char *tag, char *base, int rows, int cols)
{
int i;
printf("%s (%dx%d):\n", tag, rows, cols);
for (i = 0; i < rows; i++)
{
int j;
for (j = 0; j < cols; j++)
printf(" %c", *base++);
putchar('\n');
}
}
int main(void)
{
char smallMat[SIZE][SIZE];
char mat[ROWS][COLS];
srand((unsigned int)time(NULL));
matRandomChar(&mat[0][0], ROWS, COLS);
printMat("Big matrix", &mat[0][0], ROWS, COLS);
matRandomChar(&smallMat[0][0], SIZE, SIZE);
printMat("Small matrix", &smallMat[0][0], SIZE, SIZE);
return 0;
}
在这两个辅助函数中,代码只是一次一个字符地遍历数组数据。
样本输出:
Big matrix (3x20):
P L J T B F Y L W K C P Q X B P K V D V
K B Q P V X S B K O X C Z S U K T G F I
E X O X H C W B K W V E F U L S O F Q G
Small matrix (4x4):
U C R P
A R F U
N K Q L
Y W R M
此代码适用于仅接受 C90 以及接受 C99 或更高版本的编译器。
我可能会使用可变长度数组 (VLA) 表示法来避免重复代码,但是使用下标而不是指针是明智的。但是,可以应用相同的通用技术——尽管使用下标更为明智。下面的代码可以编译也可以不编译-DNO_PRINTING_SUBSCRIPTS——输出具有相同的组织(但是- Aor- B符号告诉你使用了哪个打印代码)。
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define SIZE 4
#define ROWS 3
#define COLS 20
void matRandomChar(int rows, int cols, char mat[rows][cols]);
void printMat(const char *tag, int rows, int cols, char mat[rows][cols]);
void matRandomChar(int rows, int cols, char mat[rows][cols])
{
char *ptr = &mat[0][0];
char *end = &mat[0][0] + rows * cols;
while (ptr < end)
*ptr++ = 'A' + rand() % ('Z' - 'A' + 1);
}
#ifdef NO_PRINTING_SUBSCRIPTS
void printMat(const char *tag, int rows, int cols, char mat[rows][cols])
{
printf("%s (%dx%d) - A:\n", tag, rows, cols);
char *ptr = &mat[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf(" %c", *ptr++);
putchar('\n');
}
}
#else
void printMat(const char *tag, int rows, int cols, char mat[rows][cols])
{
printf("%s (%dx%d) - B:\n", tag, rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
printf(" %c", mat[i][j]);
putchar('\n');
}
}
#endif /* NO_PRINTING_SUBSCRIPTS */
int main(void)
{
char smallMat[SIZE][SIZE];
char mat[ROWS][COLS];
srand((unsigned int)time(NULL));
matRandomChar(ROWS, COLS, mat);
printMat("Big matrix", ROWS, COLS, mat);
matRandomChar(SIZE, SIZE, smallMat);
printMat("Small matrix", SIZE, SIZE, smallMat);
return 0;
}
编译-DNO_PRINTING_SUBSCRIPTS
Big matrix (3x20) - A:
R K Y W Y J U T C O F N Z V H Q X T P W
Q Z R T B A O A W J O D I K F I C A R E
V X X C R P C V H E S K G C M T Y B Z D
Small matrix (4x4) - A:
V R E M
P T D G
U C Q F
D Q B F
编译-UNO_PRINTING_SUBSCRIPTS
Big matrix (3x20) - B:
L C K V S P C T L V B P G U I O Q L E B
N S C Y B I L Y G S F Z C H L Z M G A E
B P A I H E M X H V C M B Z U Y S D W A
Small matrix (4x4) - B:
O N U T
H X X P
P L M L
K R S F