0

i am learning pointers and i write these code to add two martix via function, it compile well and give correct answer for 2x2 matrix but for more than two row and col it fails when i try it with 3x3 matrix [0][0] element for the first matrix it automaticaly become 6 and [1][0] it became 9 every time, i can't figure out why this happen

#include <stdio.h> //-----------------Function------------------ void add(int (*p1)[10], int (*p2)[10], int (*p3)[10], int r, int c); void get(int (*p1)[10], int r, int c); void print(int (*p1)[10], int r, int c); //---------------Main Program---------------- int main() { //declartion int i, j, r, c; printf("\n\tenter the row and column of matrix\n\t"); scanf("%d %d", &r, &c); int m1[r][c], m2[r][c], m3[r][c]; printf("\n\tenter the element of first matrix\n\t"); get(m1, r, c); print(m1, r, c); printf("\n\tenter the element of second matrix\n\t"); get(m2, r, c); print(m2, r, c); add(m1, m2, m3, r, c); printf("\n"); print(m3, r, c); return(0); } //-------------Define Function--------------- //get() void get(int (*p1)[10], int r, int c) { int i, j; for(i = 0; i < r; i++) { printf("\n\t"); for(j = 0; j < c; j++) { scanf("%d", (*(p1+i)+j)); } } } //--------------------X---------------------- //add() void add(int (*p1)[10], int (*p2)[10], int (*p3)[10], int r, int c) { int i, j; for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { //printf("\n%d %d = %d & %d", i, j, *(*(p1+i)+j), *(*(p2+i)+j)); *(*(p3+i)+j) = *(*(p1+i)+j) + *(*(p2+i)+j); } } } //--------------------X---------------------- //print() void print(int (*p1)[10], int r, int c) { int i, j; for(i = 0; i < r; i++) { printf("\n"); for (j = 0; j < c; j++) { printf("\t%d", *(*(p1+i)+j)); } } } //--------------------X---------------------- 
7
  • 2
    Your functions expects pointers to arrays with 10 integers but your main allows different sizes Commented Aug 24, 2021 at 15:07
  • 4
    You could define void get(int r, int c, int p1[r][c]) Commented Aug 24, 2021 at 15:12
  • 2
    Alternatively, if you're enamoured of the explicit pointer to array style then void get(int r, int c, int (*p1)[c]) would work too. As long as VLAs are being used in the first place, however, I think WeatherVane's suggestion is clearer. Commented Aug 24, 2021 at 15:18
  • 2
    This p1[i][j] would be more readable than your *(*(p1+i)+j) Commented Aug 24, 2021 at 15:23
  • 1
    scanf("d", (*(p1+i)+j)); -> scanf("%d", (*(p1+i)+j)); (But agree with previous comment...) scanf("%d", p1[i][j])... Commented Aug 24, 2021 at 15:30

1 Answer 1

1
  • Drop the array pointer notation and the fixed size. Use variable-length arrays based on the parameters.
  • Remove useless comments like // get ... void get...
  • Don't use unreadable de-referncing with *(arr+i) when you could be writing readable arr[i].
  • Print new line after each row, not before each row.

Here's a cleaned up program which compiles. I haven't tested it.

#include <stdio.h> void add (int r, int c, int p1[r][c], int p2[r][c], int p3[r][c]); void get (int r, int c, int p1[r][c]); void print (int r, int c, int p1[r][c]); int main (void) { int i, j, r, c; printf("\n\tenter the row and column of matrix\n\t"); scanf("%d %d", &r, &c); int m1[r][c], m2[r][c], m3[r][c]; printf("\n\tenter the element of first matrix\n\t"); get(r, c, m1); print(r, c, m1); printf("\n\tenter the element of second matrix\n\t"); get(r, c, m2); print(r, c, m2); add(r, c, m1, m2, m3); printf("\n"); print(r, c, m3); return(0); } void get (int r, int c, int p1[r][c]) { for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { scanf("%d", &p1[i][j]); } } } void add (int r, int c, int p1[r][c], int p2[r][c], int p3[r][c]) { for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { p3[i][j] = p1[i][j] + p2[i][j]; } } } void print (int r, int c, int p1[r][c]) { for(int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { printf("\t%d", p1[i][j]); } printf("\n"); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

There is a slight typo in one of those print() calls. I guess the second one should be printing the m2 matrix?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.