This question already has an answer here:
default constructor for int [duplicate]
(1 answer)
Closed 8 years ago.
I was trying some different ways of initializing variables.
int a(0);
cout<<a;
for this code segment output is 0 .
in another way, I initialize a with 0
int a= int();
cout<<a;
output: 0
then I try this:
int a(int());
cout<<a;
this time output is 1
actually what does the value the int() function return ? 0 or 1
I think that your last attempt (int a(int())) is an example of the "most vexing parse". Thus, a is a function, not an int.
This:
#include <typeinfo>
std::cout << typeid(a).name() << std::endl;
Yields:
FiPFivEE
And putting this result here gives:
int ()(int (*)())
Doing int() creates a temporary integer and value initialize it.Value initialized integers without a specific value will have the value zero. So the declaration
int a = int();
value initializes a temporary integer, and copies it to a.
However for the third example, as many have pointed out, you actually declare a function named a.
Related
This question already has answers here:
C++ Returning Array, data loss [duplicate]
(1 answer)
error: function returns address of local variable
(8 answers)
Closed 2 years ago.
Please find the code snippet shown below:
unsigned char au8Buffer[4] = {1, 2 , 3, 4} ;//Global array declared
class abc
{
public:
unsigned char *Getheader();
}
void func(abc *ptr)
{
unsigned int a, b;
a = (ptr->Getheader())[0];
b = (ptr->Getheader())[1];
printf("the value of A = %d\n",a);
printf("the value of B = %d\n",b);
}
unsigned char * abc:: Getheader(void)
{
static int count = 0;
count++;
if(1 == count)
return &au8Buffer[0];
else
return &au8Buffer[1];
}
main()
{
abc Testobj;
func(&Testobj);
}
Can someone please tell me why is the value of the variable 'b' coming as 3?. I was expecting the value of b to be 2.
Every object and reference has a lifetime, which is a runtime property: for any object or reference, there is a point of execution of a program when its lifetime begins, and there is a moment when it ends.
The lifetime of au8Buffer ends when the function ends. Returning a pointer to its contents results in undefined values.
You can create a constructor for abc and move the initialization of au8Buffer into the constructor.
This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 3 years ago.
I'm trying to learn C++, specifically how to declare and initialize variables. I wrote this code, and I don't know why the variable c is giving a value that I have not assigned it yet.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
int a, b;
a = 1;
b = 2;
int d(4);
int result;
auto num = b;
decltype(b) c;
result = a + b - d;
cout << c;
}
The output is -2, but I didn't state c = -2 anywhere!
If you have not initialized the variable, it contains garbage value.
In C/C++, the values declared within a function represent some bytes of main memory on the cpu stack. Those bytes are usually dirty and need initialization. If you don't the values are undefined. That you're always getting '-2' is merely coincidence.
This question already has answers here:
How to increment a pointer address and pointer's value?
(5 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
void test(int x, int *y) {
*y = 5;
}
int main() {
int *a ,b =2 ;
a = &b;
test(*a, a);
a--; // a++ also give different value
This is the part i very confuse, I know if i dont put the (a--) statement the output will be 5. but what is really behind the meaning of a-- / a++ cause sometimes it give me different value as I test it with different value. I found this accidentally .
cout<<"d"<<*a<<endl;
}
a contains memory pointer to the variable. So it is *a = 5, if you increment/decrement a, then it will point to some other address/location in the memory which has some garbage value. That's what you are getting.
Lets say
a -> [2000] //memory address which contains value 5
if you do a++/a-- then a will point to [2004]/[1996] location in the memory
which has some garbage value.
This question already has answers here:
How to print function pointers with cout?
(7 answers)
Closed 5 years ago.
I define a simple function pointer pointing to a function, and when i try to output it evaluate to 1. What is happening behind the scene? (I am on mac, compiling with c++11 g++ compiler)
#include <iostream>
int foo()
{
return 5;
}
int main(int argc, char const *argv[])
{
int (*fcptr)() = foo;
std::cout<< fcptr;
return 0;
}
Output is 1.
There is no overload of operator<< which takes std::ostream and a function pointer. However, there is one that takes std::ostream and a bool, and there is implicit conversion from function pointers to bool.
So your code converts the function pointer to bool, which is defined as yielding true if it was not a null pointer; and then outputs true , which is defined as outputting 1 by default. You could do std::cout<< std::boolalpha << fcptr; to see true outputted.
This question already has answers here:
Why are references not reseatable in C++
(17 answers)
Closed 7 years ago.
According to these links: stackoverflow question and C++ FQA references cannot refer to another object/ variable well once they're initialized, but what about the below code?
// Example program in C++
#include <iostream>
using namespace std;
int main()
{
int x=10, z=12;
int &y=x;
++y;
y=z; //valid?
cout << x << ", "<< y <<", " << z << endl; // prints 12, 12, 12
return 0;
}
Below is the C code regarding pointer reseating and it seems valid, am I right?
#include <stdio.h>
int main(int argc, char *argv[])
{
int a=10, b=20;
int *ptr;
ptr = &a;
ptr = &b;
printf("%d\n",a); // prints 10
printf("%d\n",*ptr); // prints 20
return 0;
}
Can someone clear the above concept in the above two codes?
y=z; //valid?
Absolutely! However, it does not mean "y refers to z from now on". It means "set the value of z to whatever y is currently referring", which is x. Hence, y=z is another way of writing x=z.
Below is the C code regarding pointer reseating and it seems valid, am I right?
Unlike references, pointers can be re-pointed, so the re-assignment of the pointer makes it point to a different variable. Your program does not illustrate this, however, because two assignments to ptr happen without any reads of ptr in between, so only the second assignment stays.
In y=z
Its not refering to another variable its just assigning the value of z to y which is 12 and since y is reference to x, x also gets assigned value 12.
so x=y=z=12
But in pointers its valid to change the address it points to:
ptr = &a;
ptr = &b; //valid