gpt4 book ai didi

c - C 中三重指针的用途

转载 作者:行者123 更新时间:2023-11-30 19:29:23 27 4
gpt4 key购买 nike

虽然我已经看到了一些关于此的线程,但我无法理解三重指针背后的含义,因为似乎没有它们也可以做同样的事情。

void Reading(int *N, int ***M) {

printf("Input an integer N: \n");
scanf("%d", N);
*M = malloc(N * sizeof(int*));
int i;
for (i = 0; i < N; i++)
*(*M+i) = malloc(N * sizeof(int));
printf("Input N*N integers that will form a matrix \n");
int i, j;
for (i = 0; i < *N; i++)
for (j = 0; j < *N; j++)
scanf("%d", &((*M)[i][j]));
}

此代码使 **M 成为一个二维数组。如果我将 malloc 过程放入 main 中,则不再需要三重指针。有人可以解释一下为什么会出现这种情况吗?

最佳答案

If I take the malloc procedures and put them into main, the triple pointer isn't needed anymore.

将变量传递给函数有两种方法:通过值或通过引用。当您传递变量的引用时,您可以修改它。

示例:

void fun(int a) {
a = 42; // Does nothing
}

int b = 9;
fun(b);
// b is unchanged.

void fun(int *a) {
*a = 42;
}

int b = 9;
fun(&b);
// b equals 42.

你的变量恰好是一个int **类型,当你传递这个变量的地址(引用)时,这使它成为一个int ***

关于c - C 中三重指针的用途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52848702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com