该功能工作得很好。您必须错误地分配内存或将错误的参数传递给函数。
工作示例:
#include <stdio.h>
#include <stdlib.h>
int make5(int r, int c, double **d)
{
int i, j;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
d[i][j] = 5;
}
}
return 0;
}
#define ROWS 4
#define COLS 3
int main(void)
{
double** p = malloc(ROWS * sizeof(double*));
int i, j;
for (i = 0; i < ROWS; i++)
p[i] = malloc(COLS * sizeof(double));
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
p[i][j] = 1;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
printf("%f ", p[i][j]);
printf("\n");
}
make5(ROWS, COLS, p);
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
printf("%f ", p[i][j]);
printf("\n");
}
return 0;
}
输出(ideone):
1.000000 1.000000 1.000000
1.000000 1.000000 1.000000
1.000000 1.000000 1.000000
1.000000 1.000000 1.000000
5.000000 5.000000 5.000000
5.000000 5.000000 5.000000
5.000000 5.000000 5.000000
5.000000 5.000000 5.000000