0
int print(int **a, int m, int n)
{
    int i, j, sum = 0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            sum = sum + *((a + i*n) + j);
        }       
    }
    return sum;
}   

I got a garbage value instead of sum of the array elements. When I type-casted as

sum = sum + (int )*((a + i*n) + m));

I'm getting a correct answer. Why is that? But this method won't work for modifying the array elements. How can I do that? Please check this link for reference. http://ideone.com/VRVAxW

  • 2
    Could you show a) how the array was created, b) what code you tried to use for modifying things? – Scott Hunter Aug 03 '15 at 19:18
  • Your code seems wrong in your sum line: `(a + i*n)` is actually an `int **`, thus `((a + i*n) + m)` is still an `int **` (thanks to pointer arithmetics) ... so `*((a + i*n) + m)` is an `int *`, not an `int`. – NiBZ Aug 03 '15 at 19:21
  • The question has been edited from `sum = sum + *((a + i*n) + m));` to `sum = sum + *((a + i*n) + j));`. Is that answering own question? – Weather Vane Aug 03 '15 at 19:22
  • print((int **)array,2,3) I got a error when i tried (int )*((a + i*n) + m)) = 5; – Diamondhead Aug 03 '15 at 19:22
  • Well present a properly formatted question please and don't keep changing it. – Weather Vane Aug 03 '15 at 19:23
  • It was a mistake on my part i meant j not m – Diamondhead Aug 03 '15 at 19:23
  • There is a **reason** why SO asks you to post the MCVE. http://stackoverflow.com/help/mcve – Weather Vane Aug 03 '15 at 19:25
  • 1
    @Diamondhead see that question asked in the first comment? Hows about you answer that by providing an [**MCVE**](https://stackoverflow.com/help/mcve) *that **we** can compile and run* and exhibits your problem. Judging by that cast in your comment `print` call my crystal ball tells me your array being passed in to this is declared `int ar[m][n];` or similar, which is **not** compatible with `int**`, and your answer to a compiler barking errors was forcing that cast. From the looks of your [linked failure](http://ideone.com/GQwVOa) which is far more complete than your question, that's it. – WhozCraig Aug 03 '15 at 19:25
  • 1
    BY the time you get around to posting a question on SO, all such typos etc. should have long ago been resolved:( – Martin James Aug 03 '15 at 19:26

1 Answers1

1

Try

sum = sum + *( *(a + i ) + j));

Take into account that the function can be called for an array declared like

int * a[m];

or for a pointer declared like

int **a;

Otherwise you should define the function the following way provided that your compiler supports Variable Length Arrays

int print( int m, int n, int a[][n] )
{
    int i, j, sum = 0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            sum = sum + *( *(a + i ) + j );
        }       
    }
    return sum;
}  

As for this expression

 (int )*((a + i*n) + m)

that is equivalent to

 (int )*( a + i*n + m )

then in any case it is wrong.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Or the "classic" `sum += a[i][j];` – NiBZ Aug 03 '15 at 19:22
  • Yeah, it works that way, but I think it's at least worth mentioning that `*(*(a+i)+j)` is equivalent to `a[i][j]`. (And btw, you have an extra `)` in your answer, which I assume to be a typo) – NiBZ Aug 03 '15 at 19:25
  • a[i][j] doesnt work http://stackoverflow.com/questions/16724368/how-to-pass-a-2d-array-by-pointer-in-c – Diamondhead Aug 03 '15 at 19:30
  • 1
    @Diamondhead Your program referered in the first comment is simply invalid. The types of the parameter and of the argument are not compatible. – Vlad from Moscow Aug 03 '15 at 19:31
  • `a[i][j]` (or the solution given by @VladfromMoscow) works if an `int **` is given as a parameter to your function. Remember that an `int [][]` is NOT compatible with an `int **`, and that your function takes an `int **` as an argument. (This was explained in the question you linked) – NiBZ Aug 03 '15 at 19:40
  • @NiBZ Sorry a[i][j] is not working when int ** is given as a parameter.Pease check this link http://ideone.com/gCPtsT.I need the answer when int ** is given as a parameter – Diamondhead Aug 03 '15 at 19:51
  • @Diamondhead In your example, `arr` is declared as `int arr[][3]`, not as `int **arr`, and thus `arr` is not an `int **`. Your cast at line 17 does not change this. – NiBZ Aug 03 '15 at 19:53
  • @VladfromMoscow Why would someone intentionally make life hard for himself. There's not difference for a compiler anyway. – meaning-matters Aug 03 '15 at 20:05
  • @meaning-matters Each programmer should learn how to use pointers. – Vlad from Moscow Aug 03 '15 at 20:06