I'm new to C and I'm learning pointers. So, I want to pass the pointer of a 2d array. I managed to make it work, but I still get the following warning:
||=== Build: Debug in matriz (compiler: GNU GCC Compiler) ===| C:\Users\pauli\.dev\c\uvv\matriz\main.c||In function 'main':| C:\Users\pauli\.dev\c\uvv\matriz\main.c|15|warning: passing argument 1 of 'printMatriz' from incompatible pointer type [-Wincompatible-pointer-types]| C:\Users\pauli\.dev\c\uvv\matriz\main.c|4|note: expected 'int * (*)[2]' but argument is of type 'int (*)[2][2]'| C:\Users\pauli\.dev\c\uvv\matriz\main.c||In function 'printMatriz':| C:\Users\pauli\.dev\c\uvv\matriz\main.c|23|warning: format '%i' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]| ||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===| ||=== Run: Debug in matriz (compiler: GNU GCC Compiler) ===| Here is my code:
#include <stdio.h> #include <stdlib.h> #define TAM 2 void printMatriz(int *matriz[TAM][TAM]); int main() { int i, j, matriz[TAM][TAM]; for(i = 0; i < TAM; i++) { for(j = 0; j < TAM; j++) { printf("Matriz[%i][%i] = ", i, j); scanf("%i", &matriz[i][j]); } } printMatriz(&matriz); return 0; } void printMatriz(int *matriz[TAM][TAM]) { int i, j; for(i = 0; i < TAM; i++) { for(j = 0; j < TAM; j++) { printf("%i\t", matriz[j][i]); } printf("\n"); } }
int *values? Drop the*from the function prototype and definition.int *matriz[][]in function declartion means 2D array of pointers to int. In 'main' you declared it as a 2D array of just int:int matriz[][]. These are inncompatible types. Remove the '*'.