Hi, I am trying to make a program, a read from the keyboard a 2d array and print his elements. It works, but i am not so sure is very good, because when i compile, take a while and it finish with a delay. When I make debug I obtain a segmentation fault. So, I need some help. Thank you in advance. #include <stdio.h> #define ARRAY_ROW 3 #define ARRAY_COL 3 void WriteArray3(int(*piData)[ARRAY_ROW][ARRAY_COL]) { int iRow = 0; int iCol = 0; for(iRow = 0; iRow<ARRAY_ROW; iRow++) { for(iCol = 0; iCol<ARRAY_COL; iCol++) { printf("\n Element[%d][%d] = ", iRow, iCol); scanf("%d", &piData[iRow][iCol]); } printf("\n"); }} void ReadArray3(int(*piData)[ARRAY_ROW][ARRAY_COL]) { int iRow = 0; int iCol = 0; printf("\n"); for(iRow = 0; iRow<ARRAY_ROW; iRow++) { for(iCol = 0; iCol<ARRAY_COL; iCol++) { printf(" %d ", (*piData[iRow][iCol])); } printf("\n\n"); }} int main() { int aiData[ARRAY_ROW][ARRAY_COL]; WriteArray3(&aiData); ReadArray3(&aiData); return 0;}
Hallo Dragu! Here is my suggestion: Code: #include <stdio.h> #define ARRAY_ROW 3 #define ARRAY_COL 3 void WriteArray3(int(*piData)[ARRAY_ROW][ARRAY_COL]) { int iRow = 0, iCol = 0; for(iRow = 0; iRow<ARRAY_ROW; iRow++) { for(iCol = 0; iCol<ARRAY_COL; iCol++) { printf("\n Element[%d][%d] = ", iRow, iCol); scanf("%d", &(*piData)[iRow][iCol]); } printf("\n"); } } void ReadArray3(int piData[ARRAY_ROW][ARRAY_COL]) { int iRow = 0, iCol = 0; printf("\nShow Values of array[%d][%d]\n", ARRAY_ROW, ARRAY_COL); for(iRow = 0; iRow<ARRAY_ROW; iRow++) { for(iCol = 0; iCol<ARRAY_COL; iCol++) { printf(" %d ", piData[iRow][iCol]); } printf("\n"); } printf("-----------------------\n"); } int main() { int aiData[ARRAY_ROW][ARRAY_COL] = {0}; printf("Get values\n"); WriteArray3(&aiData); ReadArray3(aiData); return 0; }