Guarda qui:
Codice PHP:
#include <stdio.h>
#include <stdlib.h>
riempi(int** matrice,int i,int j){
int x,y;
for(x = 0;x<=i-1;x++)
{
for(y = 0;y<=j-1;y++)
{
*(*(matrice+x)+y) = x+y;
printf("%d ",*(*(matrice + x)+y));
}
printf("\n");
}
}
int main()
{
const int n = 4;
int i;
int** mat = (int**)malloc(n * sizeof(int*));
for(i = 0; i < n; i++)
mat[i] = (int*)malloc(n * sizeof(int));
riempi(mat, n, n);
for(i = 0; i < n; i++)
free(mat[i]);
free(mat);
}
nota la differenza (spero di non aver fatto errori sull'allocazione dinamica, sono un pò arrugginito col C).
Tra l'altro il compilatore non ti ha dato un warning tipo questo?
codice:
main.c: In function ‘main’:
main.c:22: warning: passing argument 1 of ‘riempi’ from incompatible pointer type
main.c:4: note: expected ‘int **’ but argument is of type ‘int (*)[4]’