-1

GNU GCC compiler Here is a function: int sumsintriangle(int *a,int n) where a is a n*n matrix . for some purpose I added

if(*(a+(i+1)*n+(j+1)) > *(a+(i+1)*n+j))

condition to my code which was working properly ;as the condition is true for the correct values. but for the same code when I added

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

then it didn't work (eg;let say sum was initially 1 and *(a+(i+1)*n+(j+1) was 4 ) then summation it should be giving me 5..but it gives me 1 as output...why?? Even ,when I called the same value *(a+(i+1)*n+(j+1)) in printf function,for just an enquiry, it is giving me 4 (original value)as output ...?

Why it is , that *(a+(i+1)*n+(j+1)) is working properly with printf but when I called it with sum it gives me incorrect value?

trash
  • 3
  • 3
  • Here N is what indicate? – Logicbomb Mar 31 '15 at 12:46
  • 1
    Please show your attempted code, properly formatted. The stackoverflow.com help describes how to format. – lurker Mar 31 '15 at 12:48
  • You need to show more of the code (but still, not *all* of your code), e.g. I suppose you have a for cycle to parse your matrix, there might be some problems with the boundaries. It's actually **very strange** that you have to add 1 to the indexes. You have also show how/when you initialize sum. – Antonio Mar 31 '15 at 12:56
  • Why not use: `a[(i+1))*n+(j+1)]`? With modern compilers and modern optimizers, you should get the same results. Try a benchmark. – TomOnTime Mar 31 '15 at 13:24

2 Answers2

0

Use This code code may be its work.

*(a+(i+1))*n+(j+1)
Logicbomb
  • 527
  • 1
  • 6
  • 20
0

If you can post your function properly it could be easier to help you. but i think you have an error when you put * before your expression that will give you the content of that expression, so be sure to get the values properly. example:

      int a[]; //declare an array
      a[n] // will give you the element in position 9 of the array.
      *a // will give you the first element, cause an array can be treated as a pointer (indeed it is).

I hope this answer help you. If not please tell me. Good luck!