所以我有一个任务,我需要通过将指针操作替换为数组操作,并将字符串操作替换为字符操作来更改某些函数。现在我对指针,数组,字符串等有了基本的了解,但我不能理解我必须做什么,以及我应该如何去做。下面是代码:C编程指针和字符串操作
#include <stdio.h>
#pragma warning(disable: 4996)
// This program exercises the operations of pointers and arrays
#define maxrow 50
#define maxcolumn 50
char maze[maxrow][maxcolumn]; // Define a static array of arrays of characters.
int lastrow = 0;
// Forward Declarations
#define triple(x) x % 3 == 0
void initialization(int, int);
void randommaze(int, int);
void printmaze(int, int);
void initialization(int r, int c) {
int i, j;
for (i = 0; i < r; i++){
maze[i][0] = 'X'; // add border
maze[i][c - 1] = 'X'; // add border
maze[i][c] = '\0'; // add string terminator
for (j = 1; j < c - 1; j++)
{
if ((i == 0) || (i == r - 1))
maze[i][j] = 'X'; // add border
else
maze[i][j] = ' '; // initialize with space
}
}
}
// Add 'X' into the maze at random positions
void randommaze(int r, int c) {
int i, j, d;
for (i = 1; i < r - 1; i++) {
for (j = 1; j < c - 2; j++) {
d = rand();
if (triple(d))
{
maze[i][j] = 'X';
}
}
}
i = rand() % (r - 2) + 1;
j = rand() % (c - 3) + 1;
maze[i][j] = 'S'; // define Starting point
do
{
i = rand() % (r - 2) + 1;
j = rand() % (c - 3) + 1;
} while (maze[i][j] == 'S');
maze[i][j] = 'G'; // define Goal point
}
// Print the maze
void printmaze(int r, int c) {
int i, j;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%c", maze[i][j]);
printf("\n");
}
}
void main() {
int row, column;
printf("Please enter two integers, which must be greater than 3 and less than maxrow and maxcolomn, respectively\n");
scanf("%d\n%d", &row, &column);
while ((row <= 3) || (column <= 3) || (row >= maxrow) || (column >= maxcolumn)) {
printf("both integers must be greater than 3. Row must be less than %d, and column less than %d. Please reenter\n", maxrow, maxcolumn);
scanf("%d\n%d", &row, &column);
}
initialization(row, column);
randommaze(row, column);
printmaze(row, column);
//encryptmaze(row, column);
//printmaze(row, column);
//decryptmaze(row, column);
//printmaze(row, column);
}
这里有问题我奋力上:
重写功能randommaze(行,列)通过为所有代指针操作数组操作。除了获取指针的初始值之外,您不能使用像迷宫[i] [j]这样的索引操作。
通过将字符串操作替换为所有字符操作来重写函数printmaze(row,column)。
如果有人可以向我解释我应该做什么以及我应该如何做,我真的很感激。谢谢!
好吧,迷宫的地址是迷宫所以pMaze =迷宫获取迷宫中第一个地点的地址。 pMaze ++获取迷宫中下一个位置的地址。 (pMaze + 1)也是如此。 (迷宫+最大列)获取迷宫中第二排的地址。这是完成作业所需的全部内容。 – user3629249 2015-02-09 02:03:21
通常的做法是将#define名称中的所有字母大写,并将每个单词与'_'分开,以便建议#define MAX_COLUMN(50)等。另外,为了避免大量非常困难的调试工作,现在或在将来,#define'd的值应该被parens包围当您使用#define开始编写宏时,使用parens IE #define MIN(a,b)((a)<(b))? (a):(b)..并记住宏被调用时,在宏名称和开始的paren I.E之间没有空格。使用MY_MACRO(a)不是MY_MACRO(a) – user3629249 2015-02-09 02:08:30