在c创建一个多维数组

时间:2016-08-03 04:56:39

标签: c arrays multidimensional-array

您好,需要帮助创建一个多维数组 我是C的新人,感谢任何帮助。 这是代码

#include <stdio.h>

char init_board(int row, int col);

int main(int argc, char** argv) {
        int row = argv[3];
        int col = argv[4];
        char** board = init_board(row, col);

        for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                        printf("%d", board[i][j]);
                }
        }


        return 0;
}

char init_board(int row, int col) {

        int count = 0;
        int count2 = 0;
        char** out;
        while (count < row) {
                while (count2 < col) {
                        out[count][count2] = ".";
                        count2++;
                }
                count++;
                count2 = 0;
        }
        return out;
}

任何想法如何解决这个问题?我做错了什么? 当我编译时,我收到以下警告,当我运行它时说分段错误

s@ss:~/s216/arc/ass1/ass1$ gcc ass1.c -std=c99 -o test
ass1.c: In function ‘main’:
ass1.c:6:12: warning: initialization makes integer from pointer without a cast [enabled by default]
  int row = argv[3];
            ^
ass1.c:7:12: warning: initialization makes integer from pointer without a cast [enabled by default]
  int col = argv[4];
            ^
ass1.c:8:17: warning: initialization makes pointer from integer without a cast [enabled by default]
  char** board = init_board(row, col);
                 ^
ass1.c: In function ‘init_board’:
ass1.c:27:23: warning: assignment makes integer from pointer without a cast [enabled by default]
    out[count][count2] = ".";
                       ^
ass1.c:33:2: warning: return makes integer from pointer without a cast [enabled by default]
  return out;
  ^
s@ss:~/s216/arc/ass1/ass1$ ./test x x 5 5
Segmentation fault
s@ss:~/s216/arc/ass1/ass1$

3 个答案:

答案 0 :(得分:2)

argv[3]argv[4]的类型为char*。因此,行

int row = argv[3];
int col = argv[4];

错了。您正尝试使用int初始化两个char*个对象。

如果您使用以下方式运行程序:

program 10 20 30 40

然后,

argv[0]的值为"program" argv[1]的值为"10" argv[2]的值为"20" argv[3]的值为"30" argv[4]的值为"40"

是的,它们看起来像是命令行中的数字,但它们是C程序中的字符串。您可以使用atoi从字符串中提取整数。

int row = atoi(argv[3]);
int col = atoi(argv[4]);

确保使用

#include <stdlib.h>

获取函数的声明。

答案 1 :(得分:1)

当您编写int row = argv[3];时,您正在创建一个int并将其初始化为argv[3]的值。但是,argv的类型为char **,它是指向指针的指针,或者实际上是类型为char *的数组。这是一个无效的赋值,你需要创建一个char缓冲区来读取参数,然后使用atoi(argv[i])来获取整数值。

答案 2 :(得分:0)

最初代码有很多错误,我相信下面的代码应该是什么......

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

char **init_board(int row, int col);

int main(int argc, char** argv) {

        int row = atoi(argv[3]);
        int col = atoi(argv[4]);
        char** board = init_board(row, col);

        for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                        board[i][j] = 'x';
                        printf("%c", board[i][j]);
                }
        }


        return 0;
}

char **init_board(int row, int col) {

        char** out = malloc(sizeof(char*) * row);
        for (int i = 0; i < row; i++) 
             out[i] = malloc(sizeof(char) * col);

        return out;
}