Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Pointers


ghost's Avatar
0 0

So i've started working with pointers and i pretty much just died a little on the inside, i was wondering if anyone could help explain to me how this bit of code functions :```markup#include <iostream> using namespace std;

int main () { int firstvalue = 5, secondvalue = 15; int * p1, * p2;

p1 = &firstvalue; // p1 = address of firstvalue p2 = &secondvalue; // p2 = address of secondvalue *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p1 p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20

cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0; }```

i understand that the * operator points to the value assigned, but i just dont see how the output will come out as firstvalue=10 and secondvalue=20, if anyone could explain this i would be very greatful


ghost's Avatar
0 0

EpsilonX wrote: So i've started working with pointers and i pretty much just died a little on the inside, i was wondering if anyone could help explain to me how this bit of code functions :```markup#include <iostream> using namespace std;

int main () { int firstvalue = 5, secondvalue = 15; int * p1, * p2;

p1 = &firstvalue; /* p1 points to firstvalue, and firstvalue = 5. Therefore *p1 equals 5, *p1 references the value of what it points to / p2 = &secondvalue; / same with p2 and secondvalue. p2 points to secondvalue, *p2 equals 15 */ p1 = 10; / 10 is assigned to the value pointed to by p1. This does not modify p1 at all, but instead modifies firstvalue, because p1 points to firstvalue */ *p2 = p1;
/
p2 points to secondvalue, and p1 to firstvalue, so this is the same thing as saying secondvalue = firstvalue; / p1 = p2; / no dereferencing this time, this makes it so that p1 now points to the same thing as p2, which is secondvalue, so both p1 and p2 point to secondvalue p1 = 20; / now what pt points to (secondvalue) is assigned a value of 20. Now firstvalue = 10 from before, and secondvalue is assigned 20 here. */ cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0; }```

i understand that the * operator points to the value assigned, but i just dont see how the output will come out as firstvalue=10 and secondvalue=20, if anyone could explain this i would be very greatful

I'm pretty much stepping through what was already commented, but it might help ><


ghost's Avatar
0 0

ahhh thanks, that was actually extremely helpful


ghost's Avatar
0 0
#include &lt;iostream&gt;
using namespace std;

int main ()
{
  int firstvalue = 5, secondvalue = 15;
  int * p1, * p2;

  p1 = &firstvalue; 
  p2 = &secondvalue; 
  *p1 = 10;          
  *p2 = *p1;         
  p1 = p2;           
  *p1 = 20;          
  
  cout &lt;&lt; &quot;firstvalue is &quot; &lt;&lt; firstvalue &lt;&lt; endl;
  cout &lt;&lt; &quot;secondvalue is &quot; &lt;&lt; secondvalue &lt;&lt; endl;
  return 0;
}

firstvalue ends up as 10, and secondvalue ends up as 20.

So basically what it does, p1=10, modifies it to say taht firstvalue = 10, then *p2 = *p1; is saying that that first value is = to secondvalue ?

p1=p2; is saying that p1 points to the same thing as p2, so why does it output second value as 15 if you remove *p2 = *p1;?

The second one is just completely killing my brain!:angry:

#include &lt;iostream&gt;
using namespace std;

int main ()
{
  int numbers[5];
  int * p;
  p = numbers;  *p = 10;
  p++;  *p = 20;
  p = &numbers[2];  *p = 30;
  p = numbers + 3;  *p = 40;
  p = numbers;  *(p+4) = 50;
  for (int n=0; n&lt;5; n++)
    cout &lt;&lt; numbers[n] &lt;&lt; &quot;, &quot;;
  return 0;
}```

AH HA!, ok i just made a whole bunch of sense out of the second one, but do you not take 0 into consideration when your declaring an array, and entering information into it? such as by declaring numbers[5] your declaring that it has 5 open spaces 0,1,2,3,4, buy you still label them as if it was 1,2,3,4,5?