0

Hello im learning c++ for my exam and we have a program written on paper and we have to write what program returns im having problems with pointers and functions i know how it works until: cout << *(++c) << endl; c++; cout << *c << endl; c--; cout << *c << endl;

here is the code:

#include <iostream>
using namespace std;
void Ac (int& x, int &y) { y+=2; x=y-1;};
void Dc (int x, int y) { y-=2; x=y+1;};
int main ()
{
    int a=2, i=-2, v=3, e=8, f=4, b[]={3,6,9,1,4,7,2,5}, *c=b;
    cout << "a\n" << a << endl;
    cout << a-- << endl; cout << a << endl; cout << --a << endl;
    for (int i=1; i<(a+4); i++)
        cout << b[i] << endl;
    cout << *(++c) << endl; c++; cout << *c << endl; c--; cout << *c << endl;
    while (i<=0)
    {
        v-=i;
        cout << v+2 << endl; i++;
    }
    Ac (e,f); cout << e << endl << f << endl;
    Dc (e,f); cout << e << endl << f << endl;
    Ac (f,e); cout << e << endl << f << endl;
    return 0;
}

Also, what does y += ++x do ? (not in this program)

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
striker96
  • 11
  • 2
  • http://prntscr.com/bml2ag this is what program returns cout<<*(++c) is 6 and i dont understand how ? – striker96 Jun 29 '16 at 13:24
  • 1
    Bring out pen(cil) and paper. Draw the array `b`. Draw an arrow to the first element of the array and call the arrow "c". Then do each operation "in your head" and move the arrow when you encounter `++` and `--`. The `*` gives the number that your arrow points to. – molbdnilo Jun 29 '16 at 13:30
  • ok i understand the pointers now what about ac dc functions ? – striker96 Jun 29 '16 at 13:42

4 Answers4

0
  • *c = dereference pointer to get element at address where pointer points

  • cout << *c << endl; = print that element


  • ++c = increment pointer c (by sizeof(int) bytes) to point to next element

  • *(++c) = dereference that incremented pointer to get element at address where pointer points

  • cout << *(++c) << endl; = print that element


Community
  • 1
  • 1
PcAF
  • 1,975
  • 12
  • 20
0

I think you don't want to know what the program returns, which is zero, return 0;

but instead what output the program will produce.

So lets get over the Part you don't understand:

while (i<=0)

Since i is set to -2 the loop will be repeated 3 times, one time for -2, then be incremented to -1, run again and once again for 0.

{ v-=i; cout << v+2 << endl; i++; }

Edit:

My mistake, v -= i is v = v - i.

So the first time v will be 3 - (-2), which is 5, output will be 7.

Second time v will be 5 and become 5 -(-1), output will be 8.

Third time v will be 6 and become 6-(-0) output will be 8.

v is changed by v-=i but not by cout << v+2.

Therefor 7 8 8 as output is correct.

 Ac (e,f); cout << e << endl << f << endl;

Ac works with references of the variables (i.e. & symbol) you call it with.

This means that it will change the contents of these variables.

Before calling Ac(e,f) e=8 f=4.

After you get f = f + 2, which is 6 and e = f - 1, which, with the new value of f is now 5;

This is what will be the resulting output.

Dc (e,f); cout << e << endl << f << endl;

Dc does not work with references, it will NOT change the actual values of the variables you call it with.

So it will display the values of e and f like they were before (5 and 6)

 Ac (f,e); cout << e << endl << f << endl;

The same as in the first call of Ac, just the other way around with the new values of e and f (5 and 6)

 return 0;

like i said in my first line, this returns zero as a result of your main function.

edit: i hope i did not missinterpret "until", i thought you did understand the code except everything below this line. If this is not the case, comment and ill delete my answer.

NiKoCh
  • 31
  • 7
0

first of all you have to know that pointer can point only one variable or memory space.
here the *c=b means c points to the first byte of b[0] (not array!).
suppose address of b[0] is 5002.
now the sentence *(c++), here c is a integer pointer so it increment by 2 because integer have 2 bytes. so finally c points to the memory address 5004 which is address of b[1].
simply after c++ statement c point to the next element in the array means b[1].
& finally *(c++) return the integer value which is stored in memory pointed by c.

Neel Patel
  • 358
  • 2
  • 13
0

I just added some text to your outputs. Hopefully this will help you understand what the program is doing. Notice that any change done to the variables inside the function Ac will affect the variable outside of the function (because the parameters are passed by reference). This will not happen with function Dc (because the parameters are passed by value).

For the pointer, your *c refers to a specific value of b

#include <iostream>
using namespace std;
void Ac (int& x, int &y) { y+=2; x=y-1;};
void Dc (int x, int y) { y-=2; x=y+1;};
int main ()
{
    int a=2, i=-2, v=3, e=8, f=4, b[]={3,6,9,1,4,7,2,5}, *c=b;
    cout << "a\n" << a << endl;
    cout << a-- << endl; cout << a << endl; cout << --a << endl;
    for (int i=1; i<(a+4); i++)
        cout << b[i] << endl;

    cout << "playing with pointer" << endl;
    cout << *(++c) << endl; c++; cout << *c << endl; c--; cout << *c << endl;
    cout << "finish playing with pointer \Entering the while loop" << endl; 
    while (i<=0)
    {
        v-=i; 
        cout << v+2 << endl;
        i++;
    }
    cout << "finish the while cicle\nSome funtions calls" << endl; 
    cout << "Values of e and f beforer calling function Ac(e,f)\n"<<e << endl <<     f << endl;
    Ac (e,f); cout << "Values of e and f afeter calling function Ac(e,f)\n"<<e << endl << f << endl;
    cout << "Values of e and f beforer calling function Dc(e,f)\n"<<e << endl << f << endl;
    Dc (e,f); cout << "Values of e and f afeter calling function Dc(e,f)\n"<<e << endl << f << endl;
    cout << "Values of e and f beforer calling function Ac(f,e)\n"<<e << endl << f << endl;
    Ac (f,e); cout << "Values of e and f afeter calling function Ac(f,e)\n"<<e << endl << f << endl;
    return 0;
}
  • Before entering the while cicle values are i => -2 and v => 3 the fisrt time in the while v-=i will change v value to 5 = 3 -(-2) then you have the out put 5+2 that will be the 7 you are getting. for the next iteration the initial values will be i => -1 and v => 5 this wil result the 8. the last 8 I'm sure you know how to get now. – José Francisco Luna Perez Jun 29 '16 at 14:28