【问题标题】:Converted program to use functions and now when ran does nothing将程序转换为使用函数,现在运行时什么也不做
【发布时间】:2014-06-03 12:02:32
【问题描述】:

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

我认为这可能与我的#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 }

只是想花时间感谢这个社区中的每个人的所有支持,我希望很快我就能回报你的青睐。

【问题讨论】:

  • 请注意,我确实删除了 printf 测试语句以提供更清晰的代码视图
  • ROW_SIZE :这是不正确的,应该是一个真实的数组。改用SIZE
  • 感谢@BLUEPIXY,这是正确的。当涉及到函数时,我不能像这样传递 sizeof ,显然你必须将它作为参数传递。

标签: c arrays function char


【解决方案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 有点生锈,如果我遗漏了什么,请有人纠正我的参数语法。

【讨论】:

  • 谢谢,我认为这可能是问题所在,但我不确定。我现在就去试试,然后回复你们
  • 嘿@ZevEisenberg 你用什么编译器?
  • 我将它粘贴到 CodeRunner 中的一个空白 C 文件中,所以它是默认使用的任何内容。
【解决方案2】:

好的,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");
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多