Hello.,
Im very new to C++ but im trying to resolve a fragment of the following code:

#include <iostream>
 using namespace std;
 const int NO_STEPS = 4;

 void s(int* a, int i);

 int main ( ) {
     int i, a[NO_STEPS] = {2, 4, 1, 3};

     for ( i = 0; i < NO_STEPS; i++ )
         s( a, i );

     for ( i = NO_STEPS - 1; i >= 0; i-- )
         cout << a[i] << ' ';
     cout << "Boom!\n";

     return 0;
 }

 void s(int* a, int i) {
     int* p;
     int  x, n = NO_STEPS / 2;

     if (i != n) {
         p = a + i; // be careful - pointer addition here
         x = *p;
         *p = a[n];
         a[n] = x;
     }
 }

to understand it better i split the code into 2 parts and simplified it abit so im working only on the first loop having the following code:

#include<iostream>
using namespace std;

void s(int* a, int i);

main()
{
int i, a[4] = {2,4,1,3};

        for(i = 0; i < 4; i++)
       {
         s(a,i);
         printf("%d", a[i]);
}}

void s(int *a, int i)
{
  int *p; int x, n=2;

        if(i != 2)
        {
        p = a + i;
        x = *p;
        *p = a[2];
        a[2] = x;
}}

going through it on paper i dont fully understand the last line of function a[2] = x; we already know what the x is and we already stored it in a[2], why to restore the same value to x??? and my result was very different on paper then compilers, here are the arrays values i got after the loop is complete 2114, when the compiler got 1244, could anybody please let me know where my problem is? its driving me nuts.
Thank you very much

The code doesn't store the value of x in a[2] before that line. What makes you think it does?

when in the function p=a+i; does it mean p=0+0? since a points to the 1st element which is a 0 and i is also 0 in a first pass of the loop??? or it would be a=0 + 2(since its the first element's value)?
thank you

when in the function p=a+i; does it mean p=0+0?

Only if a and i are both zero. But a is not zero.

since a points to the 1st element which is a 0 and i is also 0 in a first pass of the loop???

No, the first element is a 2. (The array whose memory location is passed as the parameter 'a' has the elements 2, 4, 1, 3.) But you're not adding the first element -- you're adding the memory address of the first element to an integer i. The sum (a+i) will be the memory address of the element i units over from the element pointed at by 'a'. For example, (a+2) will be the memory address of the element 1, and *(a+2) will be the value 1.

or it would be a=0 + 2(since its the first element's value)?
thank you

What the fuck? How could the line "p=a+i" end up assigning a value to a?

Thank you so much for fast and detailed responce., so after statement p=a+i, does it mean that p equals 2 in the first pass? and does it mean that with statement x=*p means that x=2? i'm abit confused with these 3 lines...

thank you

Thank you so much for fast and detailed responce., so after statement p=a+i, does it mean that p equals 2 in the first pass?

No, the statement p=a+i changes the variable p so that it contains a memory address i units after a. There is no way p will equal 2, because 2 is an int, and p is an int*.

and does it mean that with statement x=*p means that x=2? i'm abit confused with these 3 lines...

If *p evaluates to 2, then that means x will contain 2. Here is a simple example:

int array[] = {2, 3, 5, 7, 11};
int* p = array;  // p contains the memory address of array[0]
int x = *p;  // *p returns whatever's at the memory address stored in p.  So it returns the value that's in array[0].  So it return 2.  Thus, the value 2 gets assigned to the variable x.
int* q = p + 2;  // q contains the memory address 2 units after p.
int y = *q;  // *q is 5, so y gets assigned 5.
Comments
Nicely Done Man.
This article has been dead for over six months. Start a new discussion instead.