0

I got this function from my teacher today :

int nums[] = { 9, 5, 4, 2, 8, 1, 3, };
int *p = nums;
int tmp_num = *(p + 2);

p = &nums[0];       
*p = *p + *(p + 1);

++p;            
++(*p);         

*(p + 2) = 7;   

p = &nums[5] + 1;   
*p = 4;             

int size = sizeof nums / sizeof nums[0];
for (int i = 0; i < size; ++i)
{
    cout << "nums[" << i << "] : " << nums[i]      << endl;
}

The result :

nums[0] : 14

nums[1] : 6

nums[2] : 4

nums[3] : 7

nums[4] : 8

nums[5] : 1

nums[6] : 4

Can someone explain how does the function work? I really don't understand how can you get those results. Thank you!

Beendy
  • 1
  • 1
    Which function are you talking about? I cannot see any function in the snippet – CocoCrisp Jun 23 '17 at 05:12
  • 1
    The posted code is very basic C++ code. If you don't understand what they do, [a good textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will be your best option. – R Sahu Jun 23 '17 at 05:19

4 Answers4

1

This description line by line can orient you what is ever line doing

//nums is an array of ints but is also the address of the 1st element in the array
int nums[] = { 9, 5, 4, 2, 8, 1, 3 };

// p is a  pointer to an int initialized to nums
// (initialized then to the address of the 1st e. in the array)
int *p = nums;

// p +2 means increase in the size of 2 integers the address of P
// meaning now, p points to the 3rd element
// *(p + 2) means I dereference that pointer and get the value of that address (4)
int tmp_num = *(p + 2);

// p gets now the address (&) of the 1st element of the array nums(the address of 9)
p = &nums[0];
// the value at that position is now added to the value of the next int 9+5,
// array is now:  { 14, 5, 4, 2, 8, 1, 3 };
*p = *p + *(p + 1);

// since p is a pointer p++ does increase by the size of one integer the address, 
// now p is pointing to element2 in the array ++p;
// dereference the pointer and now increase the value by 1. i.e. 5+1, 
   array is now:  { 14, 6, 4, 2, 8, 1, 3 };
++(*p);
// add 7 to element 4 of the array... new val is 7, array is now:  { 14, 6, 4, 7, 8, 1, 3 };
*(p + 2) = 7;
// p is now holding the address of the last element in the array
p = &nums[5] + 1;
// we set that value to 4, array is now:  { 14, 6, 4, 7, 8, 1, 4 };
*p = 4;

int size = sizeof nums / sizeof nums[0];
for (int i = 0; i < size; ++i)
{
    std::cout << "nums[" << i << "] : " << nums[i] << std::endl;
}

this is only because you should already know what a pointer is, how to dereference it and how to get set its values...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
int nums[] = { 9, 5, 4, 2, 8, 1, 3, };
int *p = nums;             // p points to the beginning of the array
int tmp_num = *(p + 2);    // take the value from the third element of the array and store it in tmp_num

p = &nums[0];              // point p to the beginning of the array (again, unnessecary)
*p = *p + *(p + 1);        // Add first element (*p) and second element (*(p+1)) and store the result in the first element (first element 9 becomes 14)

++p;                       // increment pointer -> point to next address. As it's currently still pointing to the first element, p now points to the second element
++(*p);                    // increment the value p is pointing to. 5 becomes 6.

based on this you should be able to figure the rest out yourself. R Sahu is right. This is basic stuff, you should try a good book!

mattideluxe
  • 468
  • 2
  • 12
0

This is really basic stuff. However, along with mattideluxe's answer, here is a detailed "go-through" of what is happening, hope you will pick up quickly.

int nums[] = { 9, 5, 4, 2, 8, 1, 3, };
int *p = nums; // p is a pointer, which points to the first element (which is 9)
int tmp_num = *(p + 2); // p + 2 is another pointer, pointing to the third element (4)

p = &nums[0]; // again, p is assigned to pointing to the first element (which is 9)
// the above line is a duplication of "p = nums;" (second line)
*p = *p + *(p + 1); // the value pointed by p increments by the value pointed by (p + 1)
// the above line's effect, nums[] = {9 + 5, 5, 4, 2, 8, 1, 3}
// which is {14, 5, 4, 2, 8, 1, 3}

++p; // pointer p shift to the right by one position
// right now p points to the second element (which is 5)      
++(*p); // the second element increments by one
// now nums is {14, 6, 4, 2, 8, 1, 3}

*(p + 2) = 7; // the 4th element is assigned to 7
// now nums is {14, 6, 4, 7, 8, 1, 3}

p = &nums[5] + 1; // now pointer p points to the very last element
*p = 4; 
// now nums is {14, 6, 4, 7, 8, 1, 4}
xhg
  • 1,850
  • 2
  • 21
  • 35
0

Draw the array and the pointers on a piece of paper and update the drawing as you trace through the code.

Something like this:

        nums : | 9 | 5 | 4 | 2 | 8 | 1 | 3 |
------------------------------------    
p = &nums[0];

                    p+1
                     v
        nums : | 9 | 5 | 4 | 2 | 8 | 1 | 3 |
                 ^
                 p

*p = *p + *(p + 1);
[*p is 9; *(p+1) is 5; *p + *(p+1) is 14, so *p = 14]

        nums : | 14 | 5 | 4 | 2 | 8 | 1 | 3 |
                 ^
                 p
--------------------------------------
++p;
        nums : | 14 | 5 | 4 | 2 | 8 | 1 | 3 |
                      ^
                      p
---------------------
++(*p);
        nums : | 14 | 6 | 4 | 2 | 8 | 1 | 3 |
                      ^       
                      p      
-----------------
*(p + 2) = 7;

        nums : | 14 | 6 | 4 | 7 | 8 | 1 | 3 |
                      ^       ^
                      p      p+2
----------------
p = &nums[5] + 1;

        nums : | 14 | 6 | 4 | 7 | 8 | 1 | 3 |
                                          ^
                                          p
----------------  
*p = 4;

        nums : | 14 | 6 | 4 | 7 | 8 | 1 | 4 |
                                          ^
                                          p
molbdnilo
  • 64,751
  • 3
  • 43
  • 82