I have written a code in c++ to check the variable increment (Screenshot added). In line 13 when I use "++x" in printing function to print the value of x and y. The value I'm getting is not equal but the memory address is same.
In line 17 I've incremented y as ++y and I got my expected equal ans
(Screenshot added) My Code Screenshot.
What is the reason for not getting the unexpected ans in line 13?
My Code: https://gist.github.com/mefahimrahman/7fb0f45ae1f45caeb97d5aaeb39c4833
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x = 7, &y = x;
cout << "Whithout Increment: ";
cout << &x << " " << &y << endl;
cout << x << " " << y << endl;
--x;
cout << "After x Increment: ";
cout << &x << " " << &y << endl;
cout << ++x << " " << y << endl;
y++; cout << "After y Increment: ";
cout << &x << " " << &y << endl;
cout << x << " " << ++y << endl;
}
You are assuming that in
cout << ++x << " " << y << endl;
++x will be evaluated before the value of y is accessed. In other words you are assuming that your output expression is evaluated left to right. But this is not necessarily the case. Change your code to this
++x;
cout << x << " " << y << endl;
and you will get the output you expect.
Also newbies sometimes assume that ++x means the x will be incremented before anything else. But again this is not true, ++x just means that x will be incremented before the value of x is taken, not before anything else.
Related
I cant figure out why the output of this program is what it is. Maybe some one can help me out.
And why does the reference of double Pointer: 0062FB78?
Why does the reference of dereferenced double pointer = 0062FAA0?
Should'nt these be flipped?
0062FB78 is the address of x
I am guess 0062FAA0 is the address of the double Pointer?
#include <iostream>
void print(int x) {
std::cout << "value: " << (x) << "\n";
std::cout << "reference: " << (&x) << "\n";
//std::cout << (*x) << "\n";
}
void printPointer(int *x) {
std::cout << "value: " << x << "\n";
std::cout << "reference: " << &x << "\n";
std::cout << "dereference:" << *x << "\n";
}
void printDoublePointer(int **x) {
std::cout << "value: " << x << "\n";
std::cout << "reference: " << &x << "\n";
std::cout << "dereference:" << *x << "\n";
printPointer(*x);
}
void printTripplePointer(int ***x) {
std::cout << "value:" << x << "\n";
std::cout << "reference:" << &x << "\n";
std::cout << "dereference:" << *x << "\n";
printDoublePointer(*x);
}
void print(char* string) {
std::cout << "\n" << string << "\n";
}
int main()
{
int x = 19;
int *y; // y is a address space
y = &x; // &y now points to the address of x, *y now has the value of x
int **doublePointer = &y;
print(x);
printPointer(y);
printDoublePointer(doublePointer);
print("doublePointer");
std::cin >> x;
}
x
value: 19
reference: 0062FBB78
y
value: 0062FC7C
reference: 0062FBB78
defererence: 19
doublePointer
value: 0062FC58
reference of double Pointer: 0062FB78
dereference of doble Pointer: 0062FC7C
value of dereferenced double pointer: 0062FC7C
reference of dereferenced double pointer: 0062FAA0
dereference: 19
Before going over you problem, let's first agree that after calling y= &x, y is not a reference to x, but rather the address of x.
Now, let's examine the call to print
If you pay close attention, we pass the variable by-value, so this method will actually print the value 19, but the address will belong to a temp copy of x.
If we would have changed the prototype to the following one, the address of x printed here will be equal to the address of y printed in the method printPointer
void print(int & x) {
std::cout << __PRETTY_FUNCTION__ << "\n";
std::cout << "value: " << (x) << "\n";
std::cout << "reference: " << (&x) << "\n";
}
Regarding your other concern, these too occur because you pass the pointers by-value and not by-reference.
This simple program shows that everything works just fine:
int main()
{
int x = 19;
int *y = &x;
int **z = &y;
std::cout << x << "\t" << &x << std::endl;
std::cout << y << "\t" << &y << "\t" << *y << std::endl;
std::cout << z << "\t" << &z << "\t" << *z << std::endl;
}
This question already has answers here:
Order of evaluation of arguments using std::cout
(5 answers)
Strange output, not as expected
(2 answers)
Undefined behavior and sequence points
(5 answers)
Closed 5 years ago.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 5;
int& b = a;
int* c = &a;
cout << "CASE 1" << endl;
cout << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl << endl;
cout << "CASE 2";
a = 5;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << ++b << endl << "c is " << *c << endl << endl;
cout << "CASE 3";
a = 5;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << b++ << endl << "c is " << *c << endl;
}
The output:
CASE 1:
a is 5. b is 5. c is 5.
a is 10. b is 10. c is 10.
CASE 2:
a is 5. b is 5. c is 5.
a is 11. b is 11. c is 10.
CASE 3:
a is 5. b is 5. c is 5.
a is 11. b is 10. c is 10.
I understand CASE 1. But I am having difficulty understanding CASE 2 and CASE 3. Can someone explain why doesn't c get updated with new value in both cases?
The evaluation order of operands is unspecified, and you're modifying an object and reading it without those operations being sequenced.
Thus, your program is as undefined as cout << a << a++;, and anything can happen.
I think your problem is due to what is called sequence points. You can have a long read about that in this answer but in a few words it basically states the order or evaluation of the elements of an expression.
Update in your case this order is undefined, although some compilers seem to make it right-to-left.
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 months ago.
This is my assignment : " Write a function that finds the larger of two integers input (in the main program) but allows you to change the value of the integers in the function using pass by reference." I been trying to find out whats wrong with my code but I cant find out what it is. Someone please help me !! this assignment is due tonight!!
Here is my code as of right now :
#include <iostream>
using namespace std;
int change(int& x, int& y)
{
int temp=0;
cout << "Function(before change): " << x << " " << y << endl;
temp = x;
x = y;
y = temp;
cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
int x,y;
cout << " Please Enter your first number: ";
cin >> x;
cout << " Please Enter your second number: ";
cin >> y;
if (x > y)
cout << x << " is greater than " << y << endl;
if (x < y)
cout << y << " is greater than " << x << endl;
if (x == y)
cout << x << " is equal to " <<y << endl;
cout << "Main (before change): " << x << " " << y << endl;
change(x, y);
cout << "Main (after change): " << x << " " << y << endl;
system("Pause");
return 0;
}
change is declared as returning an int, but it never returns anything. It doesn't look like your function should return anything, so just declare it as void:
void change(int& x, int& y)
So I am completely new to C++ and attempting to use Xcode to write this code that analyzes two integers. I keep getting an error on the "if" line that says "use of undeclared identifier x, and "use of undeclared identifier y" I also get an error on the "else" line that says "Expected expression". What do I need to do/change to make this program work? Help.
#include <iostream>
using namespace std;
void swap (int &x, int &y);
int main()
{
cout << "Please enter two integers:/n";
cout << "First integer ==> " " >> x >> ";
cout << "Second integer ==> " " >> y >> ";
cout << endl;
if ( &x < &y )
cout << "Your integers are in the correct order:/n";
cout << " << x << " " << y << ";
else void swap(int &x, int &y);
cout << "Your integers have been swapped:/n";
cout << " << x << " " << y << ";
}
The variables x and y do not exist in the scope of the function main.
You just declared those variables in the function declaration swap, and happen to name those variables x and y. That's what these errors mean. The variables do not exist in the current scope.
use of undeclared identifier x
use of undeclared identifier y
You also do not ever define that function, so you are likely name-shadowing the std version of swap.
You need to declare and assign x and y inside a function (or outside) that refers to the values:
for example
int main()
{
int x = 5, y = 10;
if ( &x < &y )
cout << "Your integers are in the correct order:/n";
cout << " << x << " " << y << ";
else void swap(int &x, int &y);
//...
}
edit:
#include <iostream>
using namespace std;
void swap (int &x, int &y);
int main()
{
int x = 0, y = 0;
cout << "Please enter two integers:/n";
cout << "First integer ==> " " >> x >> ";
cout << "Second integer ==> " " >> y >> ";
cout << endl;
if ( x < y )
{
cout << "Your integers are in the correct order:/n";
cout << " << x << " " << y << ";
}
else
{
swap(int &x, int &y);
cout << "Your integers have been swapped:/n";
cout << " << x << " " << y << ";
}
return 0;
}
void swap (int &x, int &y)
{
// define here
}
I want to input data from txt file.
the file contains 2-d array [5][5]
how can i print out the any value i want?
i don't want to print out the whole 5*5 data
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double distance[5][5] ;
string line;
ifstream ratefile;
ratefile.open("a.txt");
ofstream file;
if (ratefile.is_open())
{
while (! ratefile.eof() )
{
getline (ratefile,line);
ratefile.getline(distance, 25, '*');
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
cin.get();
return 0;
}
If you only want to output one value and the user should be able to choose a value, you can do something like this:
int x, y;
cin >> x;
cin >> y;
cout << "\nDistance [" << x << "][" << y << "]" << ": " << distance[x][y];
But you should check if the user enter valid numbers (0 <= x < 4 and 0 <= y < 4)
There is part of the code missing, but you are printing values you want. Simply remove the lines you don't want to print.
Of course you can also use variables:
int x = 2,y = 2;
cout << endl << "Distance [" << x << "][" << y << "] : " << distance[x][y];