gpt4 book ai didi

C 编程指针和字符串操作

转载 作者:行者123 更新时间:2023-11-30 17:20:56 26 4
gpt4 key购买 nike

因此,我有一个作业,需要通过用指针操作代替数组操作、用字符串操作代替字符操作来更改某些函数。现在我对指针、数组、字符串等有了基本的了解,但我不明白我必须做什么,以及我应该如何去做。这是代码:

#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(row, column)。除了获取指针的初始值之外,您不能使用像 maze[i][j] 这样的索引操作。

  • 重写函数 printmaze(row, column),用字符串操作替换所有字符操作。

  • 如果有人可以向我解释我应该做什么以及应该如何做,我将非常感激。谢谢!

    最佳答案

    问题 2:

    数组可以用作指向其第一个成员的指针。例如,array[0]*array 返回相同的内容 - 数组第一个元素的值。由于数组是连续的内存块,因此如果您递增(或添加偏移量)指向数组开头的指针,则您将指向数组的下一个元素。这意味着 array[1] 和 *(array + 1) 是同一件事。

    如果您有一个迭代索引数组的 for 循环,您也可以使用指针增量来编写它。示例:

    /* Loop indexing an array */
    int my_array [10];
    int i = 0;
    for(; i < 10; ++i) {
    my_array[i] = 0;
    }

    /* Loop by offsetting a pointer */
    int my_array [10];
    int i = 0;
    int *ptr = my_array; /* Make it point to the first member of the array*/
    for(; i < 10; ++i) [
    *(ptr + i) = 0;
    }

    /* Looping by incrementing the pointer */
    int my_array [10];
    int *ptr = my_array; /* Make it point to the first member of the array */
    int *end_ptr = my_array + 10; /* Make a pointer pointing to one past the end of the array */
    for(; ptr != end; ++ptr) [
    *ptr = 0;
    }

    所有这些代码示例都执行相同的操作。将 0 分配给数组的所有成员。如果您有一个多维数组,请记住它仍然只是一个连续的内存块。

    问题3:

    这个问题对我来说不太清楚,所以我对你期望做什么的解释可能有点偏差,但由于你只是使用 printf 来打印单个字符,我我猜测您应该使用函数来输出单个字符。类似于 putchar

    希望这能引导您走向正确的方向。

    关于C 编程指针和字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28401473/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com