简体   繁体   English

在C中动态分配2D数组

[英]Dynamically allocating a 2D array in C

I've been reading around and I've been applying what I've been reading to my code but I am not sure if I am missing something.. the 2d array is suppose to mirror sudoku. 我一直在阅读,我一直在将自己正在阅读的内容应用到我的代码中,但是我不确定是否丢失了某些东西。.2d数组应该映射数独。 I know the problem area is in my arrayMake function. 我知道问题所在在我的arrayMake函数中。 My professor recommended using a cast with the malloc call so: sudoku = (int**)malloc(sudokus*sizeof(int*)); 我的教授建议对malloc调用使用sudoku = (int**)malloc(sudokus*sizeof(int*));sudoku = (int**)malloc(sudokus*sizeof(int*)); but that did not work for me. 但这对我没有用。

int main(){
    int sudokus;
    int** sudoku;
    sudokus = getUserInfo();
    sudoku = arrayMake(sudokus);
    /*for (int i = 0; i < (SIZE*sudokus), i++;){
        for (int j = 0; j < SIZE, j++;){
            printf("Numbers[%d][%d]:%d", i, j, sudoku[i][j]);
        }
    }*/
    system("pause");
    return 0;
}

int getUserInfo(){
    int sudokus;
    printf("How many Sudokus are you checking today?\n");
    scanf("%d{^\n]\n", &sudokus);

    return sudokus;
}

int** arrayMake(int sudokus){
    int **sudoku;
    int realsize;
    realsize = 9 * sudokus;

    sudoku = malloc(realsize*sizeof(int*));
    if (sudoku == NULL){
        printf("Memory allocation failed");
        return 0;
    }
    for (int i = 0; i < realsize, i++;){
        sudoku[i] = malloc(9 * sizeof(int));

        if (sudoku[i] == NULL){
            printf("Memory allocaiton failed");
            return 0;
                            }

    }

    return sudoku;
}

My professor recommended using a cast with the malloc call so: sudoku = (int**)malloc(sudokus * sizeof(int*)); 我的教授建议对malloc调用使用强制类型转换:sudoku =(int **)malloc(sudokus * sizeof(int *)); but that did not work for me. 但这对我没有用。

To dynamically allocate for 2D array, you usually need to do two steps. 要动态分配2D数组,通常需要执行两个步骤。 Your code is not clear as you include a realsize = 9 * sudokus which doesn't make sense. 您的代码不清楚,因为其中包含了realsize = 9 * sudokus ,这没有任何意义。 Anyway, for simplicity, lets assume your sudoku is a 3x3 matrix. 无论如何,为简单起见,假设您的数独是一个3x3矩阵。 You'll need to: 您需要:

  1. Allocate for the pointer to pointer to int: 分配指向int的指针:

     int **sudoku = malloc( 3 * sizeof( int * ) ); 
  2. Allocate for each of the individual pointer to int: 为每个单独的指针分配给int:

     for( int i = 0; i < 3; i++ ) sudoku[i] = malloc( 3 * sizeof( int ) ); 

From what I see your problem exists in your for loops where you have: 从我看来,您的问题存在于您的for循环中:

for (i = 0;i < realsize , i++)

when you really meant: 当你真正的意思是:

for (i = 0;i < realsize ; i++) ^ Note the change of , to ; for (i = 0;i < realsize ; i++) ^请注意,到的变化;

scanf("%d{^\\n]\\n", &sudokus); is a mistake. 是一个错误。

I guess you meant the { to actually be a [ but the format string is still wrong even after that change. 我猜你的意思是{实际上是[但是即使更改后格式字符串仍然是错误的。 I think you intended to consume the rest of the input, up to and including a newline character. 我认为您打算使用其余的输入,包括换行符。 However, your format string does not actually do that. 但是,您的格式字符串实际上并没有这样做。

Scanf'ing for \\n actually means consume any amount of whitespace , so in fact this code (with the [ fix) would continue waiting for input until there was a newline, and also another non-whitespace character typed after the newline. 扫描\\n实际上意味着消耗任何数量的空格 ,因此实际上,此代码(带有[修复])将继续等待输入,直到出现换行符,并且在换行符后键入另一个非空格字符。

Better would be: 更好的是:

scanf("%d", &sudokus);
int ch;
while ( (ch = getchar()) != '\n' && ch != EOF ) { }

There are a few different ways to achieve the same goal. 有几种不同的方法可以实现相同的目标。 (Note that scanning for %d[^\\n]%c is not one of them; that string is also broken). (请注意,扫描%d[^\\n]%c并不是其中之一;该字符串也已损坏)。

Also I would suggest a different variable name than sudokus . 我也建议使用与sudokus不同的变量名称。 It's confusing having two similarly-named variables sudoku and sudokus . 具有两个名称相似的变量sudokusudokus令人困惑。 Name it something that reflects its meaning. 为其命名以反映其含义。


For allocating your array, it would be much simpler to take out the arrayMake function and write something like: 对于分配数组,取出arrayMake函数并编写类似以下内容会简单得多:

int sudoku[9][9];

(I couldn't figure out what sudokus was supposed to mean or what realsize was going to be, but you could put your intended dimension inside the square brackets there). (我无法弄清楚sudokus应该是什么意思或实际realsize是多少,但是您可以将预期尺寸放到方括号内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM