I have a problem with this Code.The answer is 26 14 26 26.
Problem is, i keep finding x as 13.After the first fun function x and xptr becomes 13 but I can not figure out how it becomes 26.
#include <iostream>
using namespace std;
int* fun(int *a){
*a = *a +5;
return a;
}
int main(){
int x, y, *xpntr, *ypntr;
x = 3;
xpntr = &x;
fun(xpntr);
y = 10 + (*xpntr / 2);
ypntr = fun(xpntr);
*ypntr = *xpntr + *ypntr;
cout << x << " " << y << " " << *xpntr << " " << *ypntr << endl;
}
Before your ypntr = fun(xpntr); line, x=8, and xpntr points to x.
Now's the tricky part: ypntr = fun(xpntr) changes the value of x to 13. And fun is returning a reference to x -- ypntr now points to x as well.
This means that both xptr and yptr will point to the same data.
Your last statement (*ypntr = *xpntr + *ypntr) simply doubles the value of x.
+---------------------------+----+----+------+------+
| code | x | y | xptr | yptr |
+---------------------------+----+----+------+------+
| x = 3; | 3 | ? | ? | ? |
| xpntr = &x; | 3 | ? | &x | ? |
| fun(xpntr); | 8 | ? | &x | ? |
| y = 10 + (*xpntr / 2); | 8 | 14 | &x | ? |
| ypntr = fun(xpntr); | 13 | 14 | &x | &x |
| *ypntr = *xpntr + *ypntr; | 26 | 14 | &x | &x |
+---------------------------+----+----+------+------+
Let's go through your code step by step
#include <iostream>
using namespace std;
int* fun(int *a){
The next line means: "get the value from the location where pointer a is pointing to, add 5 to it, and write it to the location pointer a is pointing to." This does not change the pointer itself!
*a = *a +5;
Next return the pointer a, which is just the same value as was passed to the function
return a;
}
int main(){
int x, y, *xpntr, *ypntr;
x = 3;
Assign to xpntr the memory location of x
xpntr = &x;
Call fun with xpntr, discard what it returns. x will then be 3+5 = 8
fun(xpntr);
Assign to y the result of 10 + ( value of what xpntr is pointing to / 2) – xpntr isn't changed by this. y = 10 + (8/2) = 14
y = 10 + (*xpntr / 2);
Call fun with xpntr, something happens to what xpntr is pointing to, but as we already learnt, xpntr itself is not changed in the process. The value returned by fun (which is just xpntr) is assigned to ypntr. x will be changed again to 8 + 5 = 13
ypntr = fun(xpntr);
Assign to where ypntr is pointing to (which is right now identical to xpntr to the sum of what's pointed to by xpntr and ypntr. So x will then be 13 + 13 = 26 ← this is where your 26 comes from!
*ypntr = *xpntr + *ypntr;
Print the values of where xpntr and ypntr point to.
cout << x << " " << y << " " << *xpntr << " " << *ypntr << endl;
}
Related
I have made a fun called show()
in it i have declared a static and a non -static variable ;
I am observing one thing that each time my non-static local variables are getting same memory address when i am calling it .
i just want to know that this happens always or it is just a coincident;
#include<iostream>
#include<cstring>
using namespace std;
void show()
{
static int x = 10;
int y = 20;
cout<<"Address of y = "<< &y<< endl;
cout<< "x = "<< x<< " , y = "<< y<< endl;
x++;
y++;
}
int main()
{
show();
int x = 10;
cout<< x<< endl;
cout<< "len = "<< strlen("Kumar")<< endl;
show();
show();
return 0;
}
Out put of the program
Address of y = 0xd01f5ffc6c
x = 10 , y = 20
10
len = 5
Address of y = 0xd01f5ffc6c
x = 10 , y = 20
Address of y = 0xd01f5ffc6c
x = 10 , y = 20
It's related entirely to your call stack.
I added this method to your code:
void foo() {
cout << "\nCalled from Foo.\n";
show();
}
I then called foo() from main(). My output:
-$ g++ Foo.cpp -o Foo && Foo
Address of y = 0x7ffc709566d4
x = 10 , y = 20
10
len = 5
Address of y = 0x7ffc709566d4
x = 11 , y = 20
Address of y = 0x7ffc709566d4
x = 12 , y = 20
Called from Foo.
Address of y = 0x7ffc709566c4
x = 13 , y = 20
As you can see, it moved.
You should take the time to learn about stacks and the stack pointer, and how C and C++ store local variables.
I haven't found a way to typecast where bitwise assignment operation (e.g. x |= y &z;) is involved.
Example:
#include <stdio.h>
typedef enum type1{
AA = 0,
BB = 1
} type1_e;
int main()
{
type1_e x,y;
y = type1_e(y | x); //working
y |= (type1_e)(y|x); //ERROR: as.cc:15: error: invalid conversion from ‘int’ to ‘type1_e’
}
operator | yields an int result
type1_e(y | x)
y | x is an int. You're explicitly casting that int to type1_e.
y |= (type1_e)(y|x);
is equivalent to
y = y | type1_e(y | x);
You are using operator | which yields an int result, then trying to assign that to y which is type1_e. You can't do that without casting.
To overcome that, you could do this:
y = type1_e(y | type1_e(y | x));
which is the same as:
y = type1_e(y | y | x);
which is the same as:
y = type1_e(y | x);
or:
y = static_cast<type1_e>(y | x);
You can write an overload for bitwise OR (|) that works for your enum.
You will have to use static_cast as well for proper conversion.
#include<iostream>
using namespace std;
typedef enum type1{
AA = 0,
BB = 1
} type1_e;
type1_e operator |=(type1_e& x, type1_e y)
{
return x=static_cast<type1_e>(static_cast<int>(x) | static_cast<int>(y));
}
int main()
{
type1_e x = AA, y = BB;
y = type1_e(y | x); //working
std::cout << y << '\n';
x = AA, y = AA;
y |= static_cast<type1_e>(y|x); //also works
std::cout << y << '\n';
}
See demo.
I have a task from my teacher,like :
x^2 + y^3 = z
x filled only with odd
y filled only with even
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int x,y,z;
int main(){
for (x=1;x<=20;x++){
if ((x%2==1)&&(y%2==0)){
for (y=1;y<=20;y++){
if ((x%2==1)&&(y%2==0)){
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" <<z <<"\n";
}
}
}
}
}
I try to make my own code like above ,but the only one loop is Y , x stand still with 1.
I want to make x to be looping too. What should i do?
My output expectation would be like :
1^2 + 2^3 = 9
3^2 + 4^3 = 71
5^2 + 6^3 = 241
7^2 + 8^3 = 561
9^2 + 10^3 = 1081
11^2 + 12^3 = 1849
13^2 + 14^3 = 2913
15^2 + 16^3 = 4321
17^2 + 18^3 = 6121
19^2 + 20^3 = 8361
PS. Im sorry with my bad english :D
This is what you have:
int main(){
for (x=1;x<=20;x++){
if ((x%2==1)&&(y%2==0)){
for (y=1;y<=20;y++){
if ((x%2==1)&&(y%2==0)){
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" <<z <<"\n";
}
}
}
}
}
The problem is the first if ((x%2==1)&&(y%2==0)){ check.
After the inner for loop is completed, the value of y will be 21. Hence, the above conditional evaluates to false no matter that the value of x is. As a consequence, the inner for loop is executed only once. You need to remove that first if statement.
int main(){
for (x=1;x<=20;x++){
for (y=1;y<=20;y++){
if ((x%2==1)&&(y%2==0)){
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" <<z <<"\n";
}
}
}
}
Update, in response to OP's comment
Looks like you need much simpler code.
int main(){
// Start with x = 1 and increment x by 2. It will be always be odd
for ( x = 1; x <= 20; x += 2 ){
// No need to create another loop. y is simply x+1
// Since x is odd, y will be even.
y = x+1;
// Compute the result and print it.
z = (x*x) + (y*y*y);
cout << "x^2 + y^3 =" << z <<"\n";
}
}
Because y = 21 after the inside y loop.So the x loop will not be executed after . Hope it helpful.
The question is to find the output of the follwing program.
This came out in my test and i got wrong. My answer was 4, 7, 10. The answer is 4,8,12 but i need an explanation on how it works
#include<iostream>
using namespace std;
int main ()
{
int number = 4;
int array[] = {7,8,9,10,11,12,13};
int *p1 = &number ;
int *p2 = array;
int *p3 = &array[3];
int *q[] = {p1,p2,p3};
cout << q[0][0] << endl ;
cout << q[1][1] << endl ;
cout << q[2][2] << endl ;
return 0;
}
What you have is not a multi-dimensional array (C++ doesn't really have it). What you have is an array of pointers. And pointers can be indexed like arrays.
In "graphic" form the array q looks something like this:
+------+------+------+
| q[0] | q[1] | q[2] |
+------+------+------+
| | |
v | v
+------+ | +-----+----------+----------+-----+
|number| | | ... | array[3] | array[4] | ... |
+------+ | +-----+----------+----------+-----+
v
+----------+----------+-----+
| array[0] | array[1] | ... |
+----------+----------+-----+
Some notes:
What most people call multidimensional arrays are actually arrays of arrays. Much like you can have an array of integers, or like in the case of q in your code an array of pointers to integers, one can also have an array of arrays of integers. For more "dimensions" it's just another nesting of arrays.
As for why pointers and arrays both can be indexed the same way, it's because for any array or pointer a and (valid) index i, the expressions a[i] is equal to *(a + i). This equality is also the reason you can use an array as a pointer to its first element. To get a pointer to an arrays first element one can write &a[0]. It's equal to &*(a + 0), where the address-of and dereference operators cancel each other out leading to (a + 0) which is the same as (a) which is the same as a. So &a[0] and a are equal.
q[0] is in fact p1, q[1] is in fact p2 and q[2] is p3.
Now p1 is the address of number so p1[0] is simply the value of number which you correctly computed to be 4.
p2 points to array so p2[1] is the same as array[1] which is the second element in array ot 8.
p3 points to the subarray of array starting from position 3. So p3[2] is the same as array[3 + 2] = array[5] = 12.
I have a bit of code that I'm trying to analyze to study up on pointers and I'd like to ask for some help on whether I'm interpreting it correctly and if I'm not can someone help interpret it for me because I still find pointers to be confusing. You will be able to see my interpretations right after the //.
int main()
{
int x = 5;
int y = 3;
int a[] = {5, 10, 15};
PointerMystery(&x, y, a);
cout << x << " " << y << endl;
for (int i = 0; i < 3; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
void PointerMystery(int *pa, int b, int c[]) {
// *pc will return array c
int *pc = c;
// *pa which returns the address of x will now make x = 20.
*pa = 20;
// b which was 3 is now equal to 15, therefore y = 15
b = 15;
// *pc which returned a array is now 15(im not so sure about this?)
*pc = b;
// im not sure what is happening here either?
pc += 2;
// *pc will return the value (20) - 10?
*pc = *pa - 10;
cout << *pa << " " << b << " " << *pc << endl;
for (int i = 0; i < 3; i++) {
cout << c[i] << " ";
}
cout << endl;
}
Pointer variables are just what their names implies, variables that point to something.
It might be easier to understand if you think about it graphically, and step by step. So...
When the PointerMystery function is called you have the variables pa, b and c:
+----+ +---+
| pa | --> | 5 |
+----+ +---+
+---+
| 3 |
+---+
+---+ +----+----+----+
| c | --> | 5 | 10 | 15 |
+---+ +----+----+----+
After you assign pc you have
+---+
| c | --\
+---+ \ +----+----+----+
>-> | 5 | 10 | 15 |
+----+ / +----+----+----+
| pc | -/
+----+
You then do *pa = 20; so you have
+----+ +----+
| pa | --> | 20 |
+----+ +----+
Then you do *pc = b;, where b is 15, and since pc really just points to the first element of the passed array that mean you change the first element:
+---+
| c | --\
+---+ \ +----+----+----+
>-> | 15 | 10 | 15 |
+----+ / +----+----+----+
| pc | -/
+----+
You then increase the pointer pc to point to the third element of the passed array:
+---+ +----+----+----+
| c | --> | 15 | 10 | 15 |
+---+ +----+----+----+
^
+----+ |
| pc | -------------/
+----+
You then change the value where pc is pointing to the value of where pa is pointing minus 10, and the value of where pa is pointing is 20 which gives 10 after the subtraction:
+---+ +----+----+----+
| c | --> | 15 | 10 | 10 |
+---+ +----+----+----+
^
+----+ |
| pc | -------------/
+----+
Other things that might be good to know about pointers, is that arrays decays to pointers to their first element when you pass them to a function. That's the reason that c is a pointer and where it points.
Also, you can use both array indexing syntax and pointer syntax for both arrays and pointer. You can do this for arrays since they decays to pointers to their first element, and for pointers as a short-hand for *(ptr + x). In fact, for both pointers and arrays, *(ptr_or_array + x) is equivalent to ptr_or_array[x].
The last bit also explains why adding two to the pc pointer makes it points to the third element, *(pc + 0) is equivalent to pc[0] and *(pc + 2) is equivalent to pc[2], which since array indexes are zero based is the third element.