0

Here in the code written below, when I am passing the two dimensional matrix( in this case it is nMatrix[][]) , and in the function i\I am storing that into a double pointer. Then a problem is being created in accepting the elements of the array.

If I perform the same task using dynamic memory allocation and I keep the function syntax same, then it works perfectly. I want to know the reason, if anyone can help I will be thankful to him/her.

#include<stdio.h>

int inputMatrix(int **matrix , int row, int column)
{
    int i, j, nonZero  = 0;
    printf("\n Enter the elements of the matrix : \n");
    for(i=0; i<row; i++)
    {
        for(j=0; j<column; j++)
        {
            scanf("%d",&matrix[i][j]);
            if(matrix[i][j]!=0)
                nonZero++;
        }
    }
    return nonZero;
}
int main()
{
    int nMatrix[10][10], sMatrix[10][3], nr, nc, i, j, ch, nonZero;

// input the matrix in normal form
                printf("\n Enter the number of rows : ");
                scanf("%d",& nr);
                printf("\n Enter the number of columns : ");
                scanf("%d",&nc);
                nonZero = inputMatrix(nMatrix,nr,nc);
return 0;
}

In eclipse it does not show an error, and in Code::Blocks it shows note which is

note: expected ‘int **’ but argument is of type ‘int (*)[10]’
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
  • 1
    Please don't tag c++ because it's a different language. `int **` and `int(*)[10]` are not the same. – Iharob Al Asimi Oct 17 '15 at 06:49
  • okay, what i want to say is that int ** is going to store the address which was created in the main and sent while calling the function, so when i am trying to access those memory blocks which has already been created then why is there problem? – Amit Upadhyay Oct 17 '15 at 06:53
  • @iharob can you tell me why int (*)[10] will work, i mean how does that works. – Amit Upadhyay Oct 17 '15 at 06:55
  • 1
    It's because the first `int **` is a pointer to pointers while the second is an array of pointers, their memory layout is different. Just change the signature from `int inputMatrix(int **matrix , int row, int column)` to `int inputMatrix(int matrix[10][10] , int row, int column)`. Also, always check the return value of `scanf()`, specially to read integers. – Iharob Al Asimi Oct 17 '15 at 06:57
  • @iharob, thank you so much... even i have seen same syntax in some books, this works also but i have a doubt that:- if i write int inputMatrix(int matrix[10][10] , int row, int column) , then the matrix[10][10] will become a local variable to that function and thus the changes will be only done to that local variable, but i want the changes to be done to that which is there in the main () function. – Amit Upadhyay Oct 17 '15 at 07:02
  • 1
    @AmitUpadhyay Try it. It'll work as expected. – Spikatrix Oct 17 '15 at 07:03
  • 1
    "_In eclipse it does not show an error_" -- What are the compiler flags that you use? – Spikatrix Oct 17 '15 at 07:04
  • Yes it will work, what you should not do is treat the argument as an array because it now becomes a poitner. – Iharob Al Asimi Oct 17 '15 at 07:04
  • @CoolGuy This question is asked very often, if you remember any duplicate please close it! – Iharob Al Asimi Oct 17 '15 at 07:04
  • @CoolGuy yes that is working perfectly, and i didn't know what does compiler flag means. – Amit Upadhyay Oct 17 '15 at 07:08
  • @iharob, if you find this question is asked often , then before closing this one, send me a link of that. so that i can take help from there. – Amit Upadhyay Oct 17 '15 at 07:09
  • @AmitUpadhyay Did you see `-Wall` or `-Wextra` or at least `gcc filename.c` anywhere (maybe in an option in "preferences")? – Spikatrix Oct 17 '15 at 07:09
  • And as an addition, Always keep in mind that **The name of an array gets converted (*decays*) into a pointer to its first element.** – Spikatrix Oct 17 '15 at 07:11
  • @CoolGuy that is Linux GCC – Amit Upadhyay Oct 17 '15 at 07:12
  • @AmitUpadhyay If the question is closed because it's duplicate, it will redirect to the duplicate question so you and others can find the required help. Don't worry about that :) – Iharob Al Asimi Oct 17 '15 at 07:13
  • What compiler do you use in eclipse? Isn't it GCC? FYI, GCC is available in many OSes and not only on Linux. – Spikatrix Oct 17 '15 at 07:13

0 Answers0