Strange behavior with pointers

From: John (John_at_discussions.microsoft.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 08:05:07 -0800

I have created a program that accesses a flat 2 dimensional array and have
developed a routine to sum the four neighbors of a specific row/column
position.

Here is an example, this will sum all the values of row 1, column 1 (base 0)
which is the number 5

int a[9] = {1,2,3,4,5,6,7,8,9};
int* pa = &a[3]; // row 1, column 0 (i.e. number 4)
int n = *(pa++); // n = 4
n += *(++pa); // n = 10 (4 + 6)
n += *(--pa - 3); // n = 12 (10 + 2)
n += *(pa + 3); // n = 20 (12 + 8)

Now here is the strange part if I combine all the code above into one
statement

n = *(pa++) + *(++pa) + *(--pa - 3) + *(pa + 3);

I don't get the same results, instead of 20 I get 18. Am I missing something
here is this a compiler or programming error?

Any help would be greatly appreciated



Relevant Pages

  • Re: Strange behavior with pointers
    ... > developed a routine to sum the four neighbors of a specific row/column ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Strange behavior with pointers
    ... >> developed a routine to sum the four neighbors of a specific ... This is one possible way for the compiler to come to this result. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Strange behavior with pointers
    ... > developed a routine to sum the four neighbors of a specific row/column ... The compiler is allowed to do whatever it likes. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: lsqcurvefit-converge?
    ... > I have a problem considering lsqcurvefit as a routine to fit a number ... Are you truly looking at the sum of the residuals? ... The latter is what lsqcurvefit uses. ...
    (comp.soft-sys.matlab)
  • Creating a UDF from a VBA routine...
    ... How can I convert the below routine to a UDF ... put the target value in cell B1 ... Sum = Sum + Cells ...
    (microsoft.public.excel.programming)