int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue;
p2 = &secondvalue;
*p1 = 10; //line 1
*p2 = *p1; //line 2
p1 = p2; //line 3
*p1 = 20;
cout << "firstvalue is " << firstvalue << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
My Analysis of this code:
p1 points to the first value and p2 points to the second value. Now, p1 points to value 10. My first question is :
Does this means that first value now stores 10. (line 1)
According to me, line 2 means that p2 and p1 point to the value stored at the address of the second value. AM I correct or wrong?
Lastly, I have no clue of line 3. So, need some help over there too.
Answer 1: p1 still points to firstValue, but the value of firstValue is now changed to 10
Answer 2: in line 2, secondValue is assigned the value of firstValue.
Answer 3: line 3 is assigning the pointer of secondValue to the p1, so any changes in *p1 will reflect in the changed value of secondValue.
Hope this helps.
Does this means that first value now stores 10. (line 1)
Yes.
line 2 means that p2 and p1 point to the value stored at the address of the second value.
No. It means that the value p2 points to gets a copy of the value that p1 points to. It is the equivalent of saying secondvalue = firstvalue; because p2 points to secondvalue and p1 points to firatvalue and the * in *p1 or *p2 in this context accesses the value the pointer points to.
I have no clue of line 3.
This says that p1 will now point at the same place that p2 points to. So both p1 and p2 point to secondvalue.
Forget syntax for a second and think about what those objects are.
A memory location has exactly two properties, location and content.
int firstvalue; reserves a memory location (its size does not matter here).
firstvalue = 5; stores a value there.
firstvalue is a label associated with the memory address, &firstvalue is that address.
A pointer is a variable containing an address, dereferencing with '*' gives you the content at that address.
So now
p1 = &firstvalue; //address of firstvalue stored in pointer p1
p2 = &secondvalue; // same thing for p2 and secondvalue
*p1 = 10; //line 1: value 10 to variable pointed to by p1 i.e. firstvalue
*p2 = *p1; //line 2: copy firstvalue to secondvalue
p1 = p2; //line 3: now both point to secondvalue
Ok?
Lets understand what is a pointer - Pointer is storage of address it does not store value itself in light of it lets analyze the code line by line
1) Line 1 - Defined two variables (space to hold value)
2) Line 2 - Defined two pointers (space to hold address)
3) Line 4 & 5 - assigned address of the variables to pointers p1 & p2 now p1 points to location where value 5 is stored and p2 points to a location where 15 is stored
4) Line 6 - Value 10 is stored at the location p1 is pointing this means value of the variable firstvalue is changed from 5 to 10
5) Line 7 - Value of the location pointed by p1 is copied to location pointed by p2. Now both firstvalue and secondvalue is same which is 10
6) Line 8 - Value of the p2 pointer (which is address of secondvalue variable) is assigned to p1. now p1 and p2 both points to secondvalue variable
7) Line 9 - Assigned value 20 to the location pointed by pointer p1 which is now secondvalue variable
Result - both output will show value 20 as both pointers are now pointing to secondvalue variable and it is update to 20 at Line number 9
There you go...
#include <iostream>
using namespace std;
int main()
{
int firstvalue = 5, secondvalue = 15;
int * p1, *p2;
p1 = &firstvalue;
p2 = &secondvalue;
cout << "firstvalue address: ," << &firstvalue << "p1 address: ," << p1 << "p1 Value " << *p1 << endl;
*p1 = 10; //line 1
*p2 = *p1; //line 2
cout << "p1 Value " << *p1 << "p2 Value " << *p2 << endl;
p1 = p2; //line 3
cout << "p1 address: ," << p1 << "p2 address: ," << p2 << endl;
*p1 = 20;
cout << "p1 value: " << *p1 << endl;
return 0;
}
output:
firstvalue address: 0115FA74,p1 address: 0115FA74,p1 Value 5
p1 Value 10, p2 Value 10
p1 address: 0115FA68,p2 address: 0115FA68
p1 value: 20
Sorry, but your analysis is incorrect.
Starting with the first part of your code
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue;
p2 = &secondvalue;
and your analysis of it
p1 points to the first value and p2 points to the second value.
Your analysis is roughly correct. I'll give a slightly more complete description.
p1 is a variable of type int * (i.r. a pointer to an int). The assignment p1 = &firstvalue assigns the value of p1 to be the address of the variable firstvalue. This is sometimes said to mean that the pointer p1 is assigned to point at firstvalue.
The same description applies to p2 and secondvalue.
Now, for the next statement
*p1 = 10; //line 1
and your analysis of it
Now, p1 points to value 10.
This is incorrect. p1 does not point to the value 10.
The statement, *p1 = 10 assigns the value 10 to the result of *p1. *p1 is a reference to whatever p1 points at, which (as per above) is firstvalue. Accordingly, the assignment *p1 = 10 has the effect of assigning firstvalue to the value 10.
This brings us to your first question (albeit one you wrote without a question mark).
Does this means that first value now stores 10. (line 1)
If by "first value" you mean "the variable named firstvalue", the answer here is "yes".
Now, we come to the next code statement
*p2 = *p1; //line 2
and your analysis
According to me, line 2 means that p2 and p1 point to the value stored at the address of the second value. AM I correct or wrong?
You are completely and utterly wrong here.
To better understand, let's break the statement into parts. *p1 obtains a reference to the variable pointed to by p1 - which is firstvalue (which has a value 10). *p2 obtains a reference to the variable pointed to by p2 - which is secondvalue. The assignment *p2 = *p1 therefore has the same effect as secondvalue = firstvalue.
p1 still points at (contains the address of) firstvalue. Similarly, p2 still points at secondvalue. Both variables firstvalue and secondvalue have the value 10.
Now we come to line 3
p1 = p2; //line 3
And your observation about it it
Lastly, I have no clue of line 3. So, need some help over there too.
Uh hu.
p2 is a pointer, and its value is &secondvalue - the address of secondvalue. The assignment p1 = p2 assign the value of p1 to equal the value of p2. This means that p1 and p2 both contain the value &secondvalue. (Or that both the pointers p1 and p2 point to secondvalue).
A consequence is that
*p1 = 20;
has the effect of assigning secondvalue to have the value 20. firstvalue is unaffected, and still contains the value 10.
Related
int val{ 100 };
int* ptr1 = (int*)val;
int* ptr2 = ptr1 + 5;
std::cout << ptr2 << '\n' << (int)ptr2 << std::endl;
In this code example the result of (int*)val is 00000064, but I don't understand why. I also don't understand why (int)ptr2 is 120.
Analyzing line per line:
int* ptr1 = (int*)val;
Assign the decimal value 100 to the pointer ptr1;
int* ptr2 = ptr1 + 5;
This instuction invokes undefined behaviour, the algebric operation over pointer is allowed only in array context.
std::cout << ptr2 << '\n' << (int)ptr2 << std::endl;
This instruction can print everything due to the previous undefined behaviour.
The the result of (int*)val is 00000064 because is the representation of decimal value 100 in hexadecimal notation
Step by step:
int* ptr1 = (int*)val;
After executing this instruction ptr1 has decimal value 100 (00000064 in hexadecimal).
int* ptr2 = ptr1 + 5;
Now ptr2 has the same memory adress of ptr1, shifted by 5 units. It is shifted by 5 * (4 bytes) = 20 bytes. The memory address represented by ptr2 is (00000078)
This is the reason why (int)ptr2 is 120 (100 + 20).
Can this reasoning make sense?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Pointer Example
// more pointers
#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 to by p1 = 10
Line 14 - *p2 = *p1; // value pointed to by p2 = value pointed to by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed to by p1 = 20
cout << "firstvalue is " << firstvalue << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
}
I feel like I understand everything except when I get to line 14.
My question is the pointer p1 now equal to the address space that p2 is pointing too ? Also how did firstvalue = 10 and secondvalue= 20?
Here's my line-by-line explanation:
We start out with p1 pointing to firstvalue, and p2 pointing to secondvalue. firstvalue is 5, and secondvalue is 10.
*p1 = 10;
firstvalue is now 10.
*p2 = *p1;
Takes the thing pointed to by p1 (firstvalue, with a value of 10) and assigns it to the thing p2 points to (secondvalue). firstvalue and secondvalue are now both 10.
p1 = p2;
Assigns p2 to p1. p1 and p2 now both point to secondvalue.
*p1 = 20;
Sets the thing p1 points to (secondvalue) to 20. firstvalue is 10 and secondvalue is 20 now.
*(p1++)
int array[10] = {1,2};
int *p1 = array;
*p1=24;
*p1= *(p1++);
for (int i : array)
cout << i << " ";
Output is 24 24
*(++p1)
int array[10] = {1,2};
int *p1 = array;
*p1=24;
*p1= *(++p1);
for (int i : array)
cout << i << " ";
Output is 24 2
It seems like this is the exact opposite of doing increment with values. Can someone explain what is going on here? Thanks!
There is an undefined behavior in
*p1 = *(p1++);
because, quoting ยง1.9/15:
If a side effect on a scalar
object is unsequenced relative to either another side effect on the same scalar object or a value computation
using the value of the same scalar object, the behavior is undefined.
Side effect here is an increment of p1 and value computation is a computation of address using p1.
So you shouldn't rely on the exact outcome of your examples.
*p1= *(p1++);
This just doesn't make sense. The semantic meaning of this operation is different depending on which side of the = is evaluated first. So there's no way you can make any sense out of it.
For *(p1++):
*p1 = *(p1++)
p1++ will increment p1 to point to index 1 in the array, and return the previous value of p1 (index 0). So *(p1++) will return 24, and *p1 will now equal 2. *p1 is then assigned that return value (24), so the array will be {24,24}.
for *(++p1):
*p1 = *(++p1)
++p1 will increment p1 to point to index 2 in the array, and return the current value of p1 (index 1). So *(++p1) will return 2, and *p1 will now equal 2. *p1 is then assigned that return value (2), which is the original value at the index of p1 (1), so the array will remain {24,2}
In this code why does the value of firstvalue does not came as 20. At last i have changed the value of pointer p1 to 20. So p1 should assign the value of firstvalue address to 20.
// more pointers
#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 to by p1 = 10
*p2 = *p1; // value pointed to 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 << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
}
This is the final schema:
+--+ +---+
|p1| | p2|
+--+ +---+
| |
-- |
\ v
+-----------+
|secondValue|
+-----------+
20
secondValue is 20 since you're changing the value presented in p1, which points to secondValue.
Because p1 = p2 reassigns p1 to point to secondvalue instead.
When u assignp1=p2 you assign the address pointed by p2 to p1. Now both p1 and p2 point to secondValue. This does not change firstValue.
In following case I get NumRecPrinted = 0 , that is num is 0
int main()
{
int demo(int *NumRecPrinted);
int num = 0;
demo(&num);
cout << "NumRecPrinted=" << num; <<<< Prints 0
return 0;
}
int demo (int *NumRecPrinted)
{
int no_of_records = 11;
NumRecPrinted = &no_of_records;
}
You are assigning the address to the pointer, and not the value to the pointed-to. Try it like this instead
int demo (int *NumRecPrinted)
{
int no_of_records = 11;
*NumRecPrinted = no_of_records;
}
No!
*NumRecPrinted = no_of_records;
See "*" means "value of" and "&" means "address of". You want to change the "value of" NumRecPrinted, which is why the above works. What you did was to give NumRecPrinted the "address of" num_of_records.
All you did was pointer the local pointer-to-int NumRecPrinted at a new integer inside the demo function.
You want to change the integer it points to, not change where it points.
*NumRecPrinted = no_of_records;
You can see in your version that you're taking the address of a local variable, and you know it isn't the address of that variable you care about, but its value.
As others have pointed out, the * = the value of and & = address of. So you were just assigning a new address to the pointer inside the method. You should:
*NumRecPrinted = no_of_records;
See this excellent tutorial on Pointers. E.g.:
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
You want
*NumRecPrinted = no_of_records;
That means, "set the thing NumRecPrinted points to to equal no_of_records".