-5
{
int a[10][10]={{1,2,3},{4,5,6},{7,8,9}};
int n;
n=FindSumLeavingOutRowCol(a, 3,3,1,2);
printf("%d",n);

}
int FindSumLeavingOutRowCol(int** arr, int m, int n, int k,int l)
{
int sum,i,j;
for(i=0;i<m;i++)
{
    for(j=0;j<n;j++)
    {
        if((i==k)&&(j==l))
        {
            ;
        }
        else{
            sum+=*(*(a+i)+j);
        }
    }
    }

I am getting an error on passing the 2 d array also its telling 'a' undeclared. Please help me in sorting out the bug!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    first step: format this code so it is readable. Also, we need a [mcve], so copy&paste, compile and run just works to reproduce your problem. –  Nov 06 '17 at 10:28
  • 3
    `a` is undeclared isn't a bug. You'll have bugs later when your code compiles... here it seems that `a` is declared in a different (not global) scope than the function, hence invisible. – Jean-François Fabre Nov 06 '17 at 10:29
  • 3
    btw, `a` has the type `int [10][10]`, so after adjusting to a pointer this is `int (*)[10]` and **not** `int **` as your function expects. –  Nov 06 '17 at 10:30
  • 1
    The variable `a` in `sum+=*(*(a+i)+j);` is not being declared, nor initialized at that moment. The compiler tries to find it but it is unable to do so. Perhaps you made a typo or wanted to use another variable. – KarelG Nov 06 '17 at 10:30
  • A 2D array is **not** an array of pointers! You can look at this other [answer](https://stackoverflow.com/a/47094008/3545273) from mine for more details (was C++ but it is exactly the same thing for raw arrays) – Serge Ballesta Nov 06 '17 at 10:31
  • See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) for an explanation what's the difference between 2D arrays and pointer-to-pointers (and why you shouldn't use the latter in most cases). – Lundin Nov 06 '17 at 10:39
  • If you are getting an error, you need to post the full text of that error. Also, what is that first block meant to be? Was it a function? Please post full, readable code - not incomplete, terribly formatted fragments. – underscore_d Nov 06 '17 at 10:50
  • Will help, but where's your effort? – Sourav Ghosh Nov 06 '17 at 10:52

2 Answers2

1

Arrays in expressions as for example when used as arguments are converted to pointers to their first elements.

If you have an array like this

int a[10][10];

that can be rewritten like

int ( a[10] )[10];

that is it is an array of 10 elements of the type int[10[ then in expressions it is converted to the type int ( * )[10],

So for example you can write

int ( a[10] )[10] = { {1,2,3}, {4,5,6}, {7,8,9} };
int (  *p   )[10] = a;

Thus the function should be declared like

int FindSumLeavingOutRowCol( int arr[][10], int m, int n, int k,int l);

that is the same as

int FindSumLeavingOutRowCol( int ( *arr )[10], int m, int n, int k,int l);

If the compiler supports variable length arrays and the variables m and n represent the dimensions (not the ranges) then the function can be also declared like

int FindSumLeavingOutRowCol( int m, int n, int arr[m][n], int k,int l);

or

int FindSumLeavingOutRowCol( int m, int n, int arr[][n], int k,int l);

or like

int FindSumLeavingOutRowCol( int m, int n, int ( *arr )[n], int k,int l);

Otherwise you will need to add two more parameters that specify the ranges apart from the dimensions.

Pay attention to that the variable sum is not initialized

int sum,i,j;

So the function will have undefined behavior.

And moreover the parameter is declared as having name arr not a.

And I hope the function has a return statement something like

return sum;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • `int FindSumLeavingOutRowCol( int m, int n, int arr[m][n], int k,int l);` : Since `m` and `n` represent ranges in the array, not dimensions, you need to use different arguments instead of `n` for dimension(`[n]`). – BLUEPIXY Nov 06 '17 at 12:02
  • @BLUEPIXY Thanks. I have updated my post. – Vlad from Moscow Nov 06 '17 at 12:26
0

Let's start with the simple one:

also its telling 'a' undeclared.

int FindSumLeavingOutRowCol(int** arr, int m, int n, int k,int l)
{
    // [...]
            sum+=*(*(a+i)+j);
    // [...]
}

In this line, you reference a which exists neither in file scope nor in this function. You probably mean arr instead.


But the real problem is your first question:

I am getting an error on passing the 2 d array

This is because your function expects a pointer to a pointer, which is not a pointer to (the first element of) a 2d-array.

From n1570 (latest draft for C11):

§6.7.6.3 Function declarators (including prototypes), p7:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. [...]

So, as arrays can't be passed to a function, pointers get passed instead and if you declare a function parameter as array type, it is automatically adjusted to a pointer type.

This is simple for 1d arrays. Say you have

int x[5];

and a function

void foo(int a[]);

then this function declaration is equivalent to

void foo(int *a);

and you can just pass your array like

foo(x);

because the identifier of an array also evaluates to a pointer to the first array element --> type int *.

With a 2d array, the same rules apply. You could write your function with an argument like this:

int foo(int arr[][10]);

Adjusting this to a pointer type would result in

int foo(int (*arr)[10]);

This is a pointer to a 10-element array of int. It is not a pointer to a pointer. The 10 elements of your int a[10][10] are each an array of 10 integers.