This question already has answers here:
Is there a way to access the value of a local variable that has become hidden inside another scope?
(4 answers)
Closed 4 years ago.
#include <iostream>
using std::cout;
using std::endl;
int a {1};
int main()
{
int a {2};
{
int a {3};
cout << ::a << endl;
cout << a << endl;
}
return 0;
}
the first output is 1, it's the value of a that defined before function main()
so is there any way to output the second a that defined in function main() in the inner block ?
thanks!
so is there any way to output the second a that defined in function
main() in the inner block ?
a{2} and a{3} are both in block-scope.
Thus, a{3} shadows a{2} in the inner-block.
To output the a{2} you just need to move the point-of-declaration of a{3}.
int a {2};
{
cout << ::a << endl;
cout << a << endl; // a{2} will be used here
// since a{3} is not defined here yet.
int a {3};
cout << a << endl; // a{3} will be used here
// since it's already defined at this point
}
Related
This question already has answers here:
Assigning two values with an union variable
(6 answers)
Closed 3 years ago.
I was expecting the output to look like: 10 20 10 20
But the output came like this: 10 20 20 20
What is happening behind the code?
#include <iostream>
using namespace std;
typedef struct sdata{
int a;
int b;
union udata{
int a;
int b;
}u;
}Data;
int main()
{
Data s;
s.a = 10;
s.b = 20;
s.u.a = 10;
s.u.b = 20;
cout << s.a << " " << s.b << " " << s.u.a << " " << s.u.b;
return 0;
}
Unlike a struct, a union can only hold one member at a time. Each member starts at the same memory address, so writing to one affects the other.
In this case your union has two fields of type int. So if you set one than the other will contain exactly the same value. So when you set s.u.b to 20 it also sets s.u.a to that value as well.
This question already has answers here:
Arrays passed to function in C++ giving different lengths
(5 answers)
Closed 5 years ago.
Consider the code below. This generates the following output:
Length: 64
Testfn: 8
Length again: 64
I am having trouble understanding why the output at the test-function is not equal to the output at the main function. Taking into account that the output at the test-function is equal to 8, I assume that this is because sizeof(arg) gets the size of the pointer instead of the array itself.
This would all seem very logical to me, but why doesn't sizeof give that same value in the main function? And how would I fix it? I have tried several things with dereferencing, but this does not seem to give any difference whatsoever.
#include <iostream>
void test(int arg[]) {
std::cout << "Testfn: " << sizeof(arg) << std::endl;
}
int main() {
int int_arr[16];
std::cout << "Length: " << sizeof int_arr << std::endl;
test(int_arr);
std::cout << "Length again: " << sizeof int_arr << std::endl;
}
The function parameter
void test(int arg[]) {
is adjusted to pointer to type int. That is the following function declarations
void test(int arg[16]);
void test(int arg[]);
void test(int *arg);
are equivalent.
So inside the function there is used sizeof( int * ) that depending on the using platform usually equal to either 4 or 8.
You could declare the function in C++ like
void test(int ( &arg )[16]) {
std::cout << "Testfn: " << sizeof(arg) << std::endl;
}
that is to declare the parameter as reference to the array and in this case you will get the expected result.
If you use C++11 or higher, the std::array is what you need:
#include <iostream>
#include <array>
template<size_t N>
void test(const std::array<int, N>& arg) {
std::cout << "Testfn: " << arg.size() << std::endl;
}
int main() {
std::array<int, 16> int_arr;
std::cout << "Length: " << int_arr.size() << std::endl;
test(int_arr);
std::cout << "Length again: " << int_arr.size() << std::endl;
}
#include <conio.h> // include conio.h file
#include <iostream.h> // or #include<iostream>
int main()
{
int cout = 5;
cout << cout;
return 0;
}
Why does this happen ??
The code compiles correctly but it does not give expected output when it runs
This does not give the output 5 and all other stuff
It also does not give a warning.
The following line declares an int that happens to be called cout (it is not the std::cout stream)
int cout = 5;
The << operator peforms a bit shift.
So
cout << cout;
is only performing a bit shift and not storing the result.
To clarify, have a look at the following program:
#include<iostream>
int main()
{
int cout = 5;
auto shiftedval = cout << cout;
std::cout << "cout's value is " << cout << ", and the result of the bit shift is " << shiftedval << '\n';
return 0;
}
It will output:
cout's value is 5, and the result of the bit shift is 160
What is happening behind the scenes is that operator<< has been overloaded to take an ostream on the left hand side.
By including iostream you get this function and the compiler will know what you mean if you have an ostream to the left of the << operator.
Without a library the << would just simply have been a bitwise shift operator.
Also note that if you had ill-advisedly included using namespace std; or using std::cout then cout would then mean ostream and << would trigger a call to the library operator<< function. If after adding the using declaration above you include another declaration of cout the newly declared name will hide the previous declaration and cout will now be considered an int again and we're back to the bit shift operator functionality being used.
Example:
#include<iostream>
using namespace std; // using std:: at global scope
int main()
{
int cout = 5;
auto shiftedval = cout << cout;
//the following will not compile, cout is an int:
cout << "cout's value is " << cout << ", and the result of the bit shift is " << shiftedval << '\n';
//but we can get the cout from the global scope and the following will compile
::cout << "cout's value is " << cout << ", and the result of the bit shift is " << shiftedval << '\n';
return 0;
}
You are naming your variable cout, which you are confusing with std::cout. Your example preforms a bit shift operation. Try this instead :
int main()
{
int cout = 5;
std::cout << cout;
return 0;
}
Better yet, name your variable anything else to avoid the confusion :
int main()
{
int foo = 5;
std::cout << foo;
return 0;
}
If you don't explicitly declare the std namespace, either by including using namespace std; in your code or calling std::cout then cout resolves to the local variable cout in your main() function.
Even if you do declare using namespace std; cout will still resolve to the local variable instead - this is one reason why a lot of people, books, and tutorials will recommend that you explicitly write std::whatever instead of using the namespace.
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
Can you tell me the difference between the source 1 and 2?
The book says the first one is call by address(pointer) and the second one is call by reference, but i don't exactly get those two sources.
Please explain those sources to me please, thank you in advance.
1.
#include <iostream>
using namespace std;
void absolute(int *a);
void main()
{
int a = -10;
cout << "Value a before calling the main function = " << a << endl;
absolute(&a);
cout << "Value a after calling the main function = " << a << endl;
}
void absolute(int *a)
{
if (*a < 0)
*a = -*a;
}
2.
#include <iostream>
using namespace std;
void absolute(int &a);
void main()
{
int a = -10;
cout << "Value a before calling the main function" << a << endl;
absolute(a);
cout << "Value a after calling the main function" << a << endl;
}
void absolute(int &a)
{
if (a < 0)
a = -a;
}
In terms of what happens at the CPU level, pointers and references are exactly the same. The difference lies in the compiler, it won't let you do a delete on a reference (and there's less typing)
So in your code both functions do the same thing.
This question already has answers here:
Uninitialized values being initialized?
(7 answers)
Closed 8 years ago.
I'm doing some testing...
Firstly I post my source code
the .h file
class Complex{
private:
int r = 0;//initializer
int i ;
public:
Complex(int , int I = 0);
Complex();
void print();
void set(int, int I = 1);
static void print_count();
static int count;
};
the .cpp file
#include <iostream>
#include "complex.h"
int Complex::count = 1;
Complex::Complex(int R , int I){
r = R;
i = I;
count++;
std::cout << "constructing Complex object...count is " << Complex::count << std::endl;
}
Complex::Complex(){//default constructor
std::cout << "default constructor is called..." << std::endl;
}
void Complex::print(){
std::cout << "r = " << r << ';' << "i = " << i << std::endl;
return;
}
void Complex::set(int R, int I /*= 2*/){//will be "redefaulting", an error
r = R;
i = I;
return;
}
void Complex::print_count(){//static
Complex::count = -1;//jsut for signaling...
std::cout << "count is " << count << std::endl;
return;
}
the main function
#include <iostream>
#include "complex.h"
int main(){
Complex d;//using default constructor
d.print();
/*Complex c(4, 5);*/
Complex c(4);
//c.print();
/*c.set(2, 3)*/
c.print();
c.set(2 );
c.print();
std::cout << "count is " << c.count << std::endl;//c can access member data
c.print_count();
c.count++;//
return 0;
}
consider the Complex object d constructed with default ctor
because the data member r is initialized using with 0, when executing d.print(),
r is expected to be 0
and i isn't, so I expected it to be garbage value
but when I'm testing, one strange thing happens.
if I eliminate this and the following lines of code in the main file:
std::cout << "count is " << c.count << std::endl;//c can access member data
then d.print() will give the value of i as 32767 on my system, which I guess it's a garbage value;
but once that line is added, d.print() just give i's value to 0 on my system.
I don't get it. I hasn't set, modiify or initialize i's value, why should it be 0?
or, it is also a garbage value?
or, calling one of those function corrupts the value of i?
how is the thing run behind the scene here?
thx for helping.
0 is just as garbage value as any other. Don't make the mistake of thinking otherwise.
Formally, reading an uninitialized variable is undefined behavior, so there's no point in wondering about it: just fix it by initializing the variable properly.