【问题标题】:Using scanf() with a pointer to a double pointer使用带有指向双指针的指针的 scanf()
【发布时间】:2016-11-03 05:51:46
【问题描述】:

我觉得我已经尝试了所有我知道的组合来让它工作,但无法弄清楚。我如何将scanf() 转换为作为指向函数的指针传递的int**?我尝试搜索但找不到这个,如果它是重复的,请告诉我,我会删除。它开始运行并在输入一些值后出现段错误。

这是我的代码,我认为它弄乱了 setMatrix() 函数的 scanf() 行:

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

// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {  
        int **mat = calloc(rmax, sizeof(int*));
        for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
        return mat;
}

// fill matrix
void setMatrix(int ***mat, int r, int c){
    printf("Insert the elements of your matrix:\n");
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            printf("Insert element [%d][%d]: ", i, j);
            scanf("%d", mat[i][j]); // problem here??
            printf("matrix[%d][%d]: %d\n", i, j, (*mat)[i][j]);
        }
    }   
    return;
}

// print matrix
void printMatrix(int ***mat, int r, int c){ 

    for (int i=0; i<r;i++){
        for (int j=0; j<c;j++) {
                printf("%d ", (*mat)[i][j]);
        }
        printf("\n");
    }

}

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

    int r = 3, c = 3;

    int **mat = callocMatrix(r, c);

    setMatrix(&mat, r, c);

    printMatrix(&mat, r, c);
}

【问题讨论】:

  • 请注意,标题中的“双指针”具有误导性。您指的是int **,但听起来您指的是double *

标签: c multidimensional-array scanf pass-by-pointer


【解决方案1】:

没有必要使用三重指针***。传递二维数组将按原样工作。代码如下:

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

// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
    int **mat = calloc(rmax, sizeof(int*));
    for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
    return mat;
}

// fill matrix
void setMatrix(int **mat, int r, int c){
    printf("Insert the elements of your matrix:\n");
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            printf("Insert element [%d][%d]: ", i, j);
            scanf("%d", &mat[i][j]); // no problem here
            printf("matrix[%d][%d]: %d\n", i, j, mat[i][j]);
        }
    }
}

// print matrix
void printMatrix(int **mat, int r, int c){

    for (int i=0; i<r;i++){
        for (int j=0; j<c;j++) {
                printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
}

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

    int r = 3, c = 3;

    int **mat = callocMatrix(r, c);

    setMatrix(mat, r, c);

    printMatrix(mat, r, c);
}

【讨论】:

  • 有人告诉我,如果我想编辑指向另一个函数的指针的指针,我需要将其作为指向双指针的指针传递,这是错误的还是我误解了某种方式?
  • 我刚才也尝试了scanf("%d", &amp;(*mat)[i][j]);,这也奏效了,这是一个不好的方法吗?
  • 如果您想编辑指针,这是正确的。在这种情况下,您不是在编辑指针 - 您正在更改指针指向的值。所以除非setMatrix会改变矩阵在内存中的位置,比如callocMatrix,否则不需要将矩阵作为指针传递。
  • scanf("%d", &amp;(*mat)[i][j]); 不是一个海湾方式。为什么要把事情复杂化?
  • 从技术上讲,如果您只是退出应用程序,操作系统会为您释放它。但是,在自己之后进行清理是一个好习惯。您基本上必须以相反的顺序进行释放:首先释放循环中的行,然后释放列数组。
【解决方案2】:

应该是:

scanf("%d", &(*mat)[i][j]);

您正在传递一个指向矩阵对象的指针,因此您需要取消引用它(使用*),就像使用printf 一样。 scanf 然后需要写入元素的地址,所以你需要&amp;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2019-04-19
    • 2020-11-15
    • 2013-11-28
    • 2011-08-04
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多