-2

I'm trying write a code that matrix 3*3 but it doesn't work correctly. it prints random numbers. what I did wrong?

    int matrix[3][3];   
    int i,j;

    for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
      {
        scanf("%d",&matrix[i][j]);
      }
    }
    for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
      {
        printf("%d ",&matrix[i][j]);
      }
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    https://beginnersbook.com/2014/01/2d-arrays-in-c-example/ – PM 77-1 Sep 07 '18 at 22:50
  • 2
    Compiler warnings are your friend. – Weather Vane Sep 07 '18 at 22:52
  • thanks for your help. but compiler didn't show this error – Carl Jackson Sep 07 '18 at 22:58
  • 2
    It's not printing random numbers; it is printing the address of of each element (`&matrix[i][j]`) – bigwillydos Sep 07 '18 at 23:13
  • 1
    If you'd turned on compiler warnings, like using `Wall` with `gcc`, you would have seen this: `matrix_print.c:19:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=] printf("%d ",&matrix[i][j]); ~^ ~~~~~~~~~~~~~ %n ` – bruceg Sep 08 '18 at 00:08
  • Read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Stackoverflow is not a *fix-my-bugs* site – Basile Starynkevitch Sep 08 '18 at 04:52
  • @AkınTokluoglu it shows **warnings** and not errors. [How to turn on (literally) ALL of GCC's warnings?](https://stackoverflow.com/q/11714827/995714) – phuclv Sep 08 '18 at 05:01
  • You are right. I'm new to this. I will pay attention to them from now on. – Carl Jackson Sep 08 '18 at 10:33

1 Answers1

3

You should remove & while printing values
Use below code for printing.

 printf("%d ",matrix[i][j]);
Mayur
  • 2,583
  • 16
  • 28