转换程序使用函数,现在运行时什么都不做

时间:2014-04-18 22:17:35

标签: c arrays function char

好的,在本书中我正在研究(尝试学习函数),我的编程项目是转换我在前一章中编写的以前的程序来使用函数。所以我转换它,编译它并运行它但是在函数运行后它无法执行该函数中的任何循环....所以程序运行但是我的控制台中没有打印任何内容,只需按任意键即可退出。

我认为这可能与我的#define有关,但我不确定,因为我仍在尝试学习它们,本章似乎在代码的一部分中表明宏可以在整个程序中使用,而不管函数....纠正我,如果我错了,但这里是下面的代码,我运行了一些测试,函数被调用并一直运行但for循环,这样无法运行我的printf测试...

    // Chapter 9 Programming Project #3

  1 #include <stdio.h>
  2 #include <stdbool.h>
  3 #include <stdlib.h>
  4 #include <time.h>
  5
  6 #define SIZE 10
  7 #define PATH_SIZE 25
  8 #define ROW_SIZE ((int) (sizeof(walk) / sizeof(walk[0])))
  9
 10 void generate_random_walk(char walk[SIZE][SIZE]);
 11 void print_array(char walk[SIZE][SIZE]);
 12
 13 int main(void)
 14 {
 15         char walk[SIZE][SIZE];
 16         // Create board
 17         generate_random_walk(walk);
 18         // Print board
 19         print_array(walk);
 20         return 0;
 21 }
 22
 23 void generate_random_walk(char walk[SIZE][SIZE])
 24 {
 25         // 0 = Up, 1 = Down, 2 = Left, 3 = Right
 26         int i, x, y;
 27
 28         // Generate a random number
 29         srand((int) time(NULL));
 30         int dir = rand() % 4;
 31
 32         // Set all positions of walk to '.'
 33         for (x = 0; x < ROW_SIZE; x++) {
 34                 for (y = 0; y < ROW_SIZE; y++)
 35                         walk[x][y] = '.';
 36                 printf("Set Test: %d\n", x);
 37         }
 38         x = 0;
 39         y = 0;
 40         walk[0][0] = 'A';
 41
 42         // Generate the path
 43         for (i = 0; i < PATH_SIZE;) {
 44                 // Check that the last character has not been cornered
 45                 if ((walk[x][y - 1] != '.' || y - 1 < 0) &&
 46                                 (walk[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
 47                                 (walk[x - 1][y] != '.' || x - 1 < 0) &&
 48                                 (walk[x + 1][y] != '.' || x + 1 > ROW_SIZE))
 49                         break;
 50
 51                 // Check the direction and replace that char
 52                 switch (dir) {
 53                         case 0: if ((y - 1) >= 0
 54                                                 && walk[x][y - 1] == '.') {
 55                                         walk[x][--y] = i + 'B';
 56                                         ++i;
 57                                 } break;
 58                         case 1: if ((y + 1) < ROW_SIZE
 59                                                 && walk[x][y + 1] == '.') {
 60                                         walk[x][++y] = i + 'B';
 61                                         ++i;
 62                                 } break;
 63                         case 2: if ((x - 1) >= 0
 64                                                 && walk[x - 1][y] == '.') {
 65                                         walk[--x][y] = i + 'B';
 66                                         ++i;
 67                                 } break;
 68                         case 3: if ((x + 1) < ROW_SIZE
 69                                                 && walk[x + 1][y] == '.') {
 70                                         walk[++x][y] = i + 'B';
 71                                         ++i;
 72                                 } break;
 73                         default: if (walk[x][y] == '.')
 74                                          walk[x][y] = i + 'B';
 75                                  break;
 76                 }
 77
 78                 // Reset the random directions
 79                 dir = rand() % 4;
 80         }
 81 }
 82
 83 void print_array(char walk[SIZE][SIZE])
 84 {
 85         int x, y;
 86         // Print the walk
 87         for (x = 0; x < ROW_SIZE; x++) {
 88                 for (y = 0; y < ROW_SIZE; y++)
 89                         printf("%4c ", walk[x][y]);
 90                 printf("\n");
 91                 printf("Print Test: %d\n", x);
 92         }
 93 }

只是想花点时间感谢社区中的每个人提供所有支持,我希望很快我能够回报他们。

2 个答案:

答案 0 :(得分:1)

当我尝试运行它时,我得到以下编译器警告:

Untitled.c:34:25: warning: sizeof on array function parameter will return size of 'char (*)[10]' instead of 'char [10][10]' [-Wsizeof-array-argument]
        for (x = 0; x < ROW_SIZE; x++) {
                        ^

我忘记了细节(如果你知道的话,有人请插入),但我似乎记得C处理数组传入函数的方式很奇怪。您可能想要计算数组的大小并将其作为附加参数传递给函数,如下所示:

void generate_random_walk(char walk[SIZE][SIZE], int arraySize)

我的C有点生疏,所以如果我错过了某些内容,有人请更正我的参数语法。

答案 1 :(得分:0)

好吧,Zev Eisenberg,再次感谢,这里是任何可能遇到类似问题的人的更正代码。

    // Chapter 9 Programming Project #3

    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>

    #define SIZE 10
    #define PATH_SIZE 25
    #define ROW_SIZE ((int) (sizeof(walk) / sizeof(walk[0])))

    void generate_random_walk(char walk[SIZE][SIZE], int arraySize);
    void print_array(char walk[SIZE][SIZE], int arraySize);

    int main(void)
    {
        char walk[SIZE][SIZE];
        int arraySize = ROW_SIZE;
        // Create board
        generate_random_walk(walk, arraySize);
        // Print board
        print_array(walk, arraySize);
        return 0;
    }

    void generate_random_walk(char walk[SIZE][SIZE], int arraySize)
    {
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        int i, x, y;

        // Generate a random number
        srand((int) time(NULL));
        int dir = rand() % 4;

        // Set all positions of walk to '.'
        for (x = 0; x < arraySize; x++) {
            for (y = 0; y < arraySize; y++)
                walk[x][y] = '.';
        }
        x = 0;
        y = 0;
        walk[0][0] = 'A';

        // Generate the path
        for (i = 0; i < PATH_SIZE;) {
            // Check that the last character has not been cornered
            if ((walk[x][y - 1] != '.' || y - 1 < 0) &&
                (walk[x][y + 1] != '.' || y + 1 > arraySize) &&
                (walk[x - 1][y] != '.' || x - 1 < 0) &&
                (walk[x + 1][y] != '.' || x + 1 > arraySize))
                break;

            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0
                            && walk[x][y - 1] == '.') {
                            walk[x][--y] = i + 'B';
                            ++i;
                        } break;
                case 1: if ((y + 1) < arraySize
                            && walk[x][y + 1] == '.') {
                            walk[x][++y] = i + 'B';
                            ++i;
                        } break;
                case 2: if ((x - 1) >= 0
                            && walk[x - 1][y] == '.') {
                            walk[--x][y] = i + 'B';
                            ++i;
                        } break;
                case 3: if ((x + 1) < arraySize
                            && walk[x + 1][y] == '.') {
                            walk[++x][y] = i + 'B';
                            ++i;
                        } break;
                default: if (walk[x][y] == '.')
                             walk[x][y] = i + 'B';
                             break;
            }

        // Reset the random directions
        dir = rand() % 4;
        }
    }

    void print_array(char walk[SIZE][SIZE], int arraySize)
    {
        int x, y;
        // Print the walk
        for (x = 0; x < arraySize; x++) {
            for (y = 0; y < arraySize; y++)
                printf("%4c ", walk[x][y]);
            printf("\n");
        }
    }