Basics Pointers c++ [closed] - c++

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.

Related

pointers in C++ (need overview)

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.

Value of output is different (pointer) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include<iostream>
using namespace std;
void HardToFollow(int *p, int q, int *num);
void HardToFollow(int *p, int q, int *num) {
*p = q + *num;
*num = q;
num = p;
p = &q;
cout << *p << " " << q << " " <<*num<<endl;
// value is ``1 1 4
}
main() {
int *q;
int trouble[3];
trouble[0] = 1;
q = &trouble[1];
*q = 2;
trouble[2] = 3;
HardToFollow(q, trouble[0], &trouble[2]); // 2 1 3
cout << *q << " " << trouble[0] << " " << trouble[2]<<endl;
// value become 4 1 1}
Hi everyone i am beginner to stackoverflow and I really don't get why my first output in HardToFollow function, the value is 1, 1 and 4.
But when it comes to main function output it become 4, 1 and 1. I spent much time try to understand but I can't.
Hopefully someone can help me here.
Let's look at what HardToFollow() receives and follow its flow line-by-line.
[I'll just note right here that this code is, in fact, very hard to follow, so it's possible I've made a mistake. Please follow my logic carefully to see if you agree.]
At the start of HardToFollow():
q-in-main = address of trouble[1] (value = 2)
p = address of trouble[1] (value = 2)
q-in-HardToFollow = copy of value of trouble[0] = 1
num = address of trouble[2] (value = 3)
Note that p and q-in-main are not the same variable. They are two separate pointers that point to the same location. This is because (as I'm sure is one of the points of this exercise): Variables, including pointers are passed as copies. So if you send a pointer, a new copy of the pointer is created.
*p = q + *num;
The value of the location pointed to by p is set to q-in-HardToFollow plus the value of the location pointed to by num. This is 1 plus 3, which is 4. q-in-main also points to the same location as p, so its value will also change. So now:
q-in-main = address of trouble[1] (value = 4)
p = address of trouble[1] (value = 4)
*num = q;
The value of the location pointed to by num is set to q-in-HardToFollow, which is 1. So now:
num = address of trouble[2] (value = 1)
num = p;
num is now set to point to the same location as p, that is, trouble[1]. So:
num = address of trouble[1] (value = 4)
p = &q;
p is now set to point to the location of q-in-HardToFollow. So:
p = address of q-in-HardToFollow (value = 1)
So the current addresses and values are:
q-in-main = address of trouble[1] (value = 4)
p = address of q-in-HardToFollow (value = 1)
q-in-HardToFollow = copy of value of trouble[0] = 1
num = address of trouble[1] (value = 4)
This works out well with the output you are getting.

pointers, pointer tot pointer, exc

We need to understand the next code for college. Everything goes fine till the this instruction :
(*((*q1)+2))++;
Can someone tell me what's the effect of that instruction? q1 or p1 doesn't change. Here is the whole exercise.
double a = 2;
double b[] = {1,3,5};
double c[] = {4,6,8,10};
double & d = a;
double* p1 = b;
double* p2 = &c[1];
double **q1 = &p2;
double **q2 = &p1;
(*q1)++;
a = **q1-*(p1+1);
q1 = q2;
(*((*q1)+2))++;
p2-=2;
**q1 = *p2;
*q2 = &c[1];
b[2] -= *(p2+3)-**q1;
d = c[1];
cout << a << endl << b[0] << endl << b[1] << endl << b[2] << endl;
Considering that q1 is a double** :
*q1 Dereference q1, obtaining a double*&
( )+2 Add 2, ending up pointing two double's further
*( ) Dereference where we are, obtaining a double&
( )++; Increment that last double.
Work from the inside out.
(*((*q1)+2))++;
(*q1) is p2 so replace.
(*(p2 + 2))++;
p2 + 2 is the address of c[3] so replace
(*(&c[3])++;
*(&c[3]) is 10 so replace
10++ is 11
(*((*q1) + 2))++; is equivalent to (*(p1 + 2))++;

In This code why does the value of firstvalue does came out as 20

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.

Passing pointers to function does not return value

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".