This question already has answers here:
Variable initialization in C++
(11 answers)
Closed 4 years ago.
I'm very new to C++ and have just started out.
I'm doing a simple exercise where I need to declare a variable with type int and add to it.
Essentially the integer has a starting value of 44 and I have no idea why.
The exercise comes out of a book I'm following.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int sum;
sum = sum + 1;
cout << sum;
return 0;
}
If I run the following code I get the answer of 45, which makes no sense at all to me.
I want to understand why sum has a value of 44, if no value is assigned to it.
I'm using VScode and the g++ compiler.
Thanks!
You have to initialize variable at first.
The correct code would be:
int sum = 0;
sum = sum + 1;
In your case you have undefined behavior.
Related
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
When passing an array to a function in C++, why won't sizeof() work the same as in the main function?
(1 answer)
determine size of array if passed to function
(10 answers)
Closed 3 years ago.
So I'm doing a small task with C++ (never worked with this language before) and I encountered a problem with using 'sizeof' in an array to find its length. When I use it inside main() it works as expected, but if I use it inside a function it doesn't. It's clear that I'm missing some knowledge on very basic topics of C++ so I've come to find some guidance (I have checked the related questions of stack overflow and none provides some theory on what's happening, only ways of solving it in a different way).
I would also like to point out that I've seen different ways of making this work, but I would like to know why this one is not working and learn from it, thanks!
This is the code that I'm using:
// function example
#include <iostream>
#include <vector>
using namespace std;
// functions used
float max_value_array(float array[]);
int main()
{
float rand_vec[] = {3, 2, 7, 5, 4};
float max_num = max_value_array(rand_vec);
cout << max_num;
}
float max_value_array(float array[])
{
int len_array = sizeof(array) / sizeof(array[0]);
float max_number = array[0];
for (int i = 1; i < len_array; i++)
{
std::cout << array[i] << std::endl;
if (array[i] > max_number)
{
max_number = array[i];
}
}
return max_number;
}
(<vector> is included because I'm using it in a different part of the code)
What I would expect from this code is to return 7, however I'm getting 3. Upon investigation I found out that if I return len_array in max_value_array I'm getting 1 (and thus, getting the first element of the array as the loop doesn't start), which means that either the vector is not correctly passed, or that operation does not work. However if I return array[3] I get 5, which means the array is passed correctly.
I also saw that when I return sizeof(array) I get 4, and the same value if I return sizeof(array[0]) (4). However, if I use cout in the main function to log both sizeof(rand_vec) and sizeof(rand_vec[0]) I get respectively 520 and 4, which makes me think that there is something about sizeof and arrays that I'm not understanding.
Why does this happen?
This question already has answers here:
What does the integer suffix J mean?
(2 answers)
Closed 6 years ago.
I am using Apple LLVM version 7.3.0 (clang-703.0.31) to compile some code and I find that 1j could be a constant value.
In the following code snippet, I assign int b with value 1j and it compiles.
But j is not any variable I have defined.
Can anybody explain what is 1j? Or it is a compiler bug?
Thanks a lot.
#include <iostream>
using namespace std;
int main() {
// What is 1j? Why this code can compile??
int b = 1j;
cout << b << endl;
return 0;
}
I have seen another post explaining this is complex number.
What does the integer suffix J mean?
And using compiler option -pedantic would raise a warning for it.
The suffix i and j is used for complex numbers.
This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main()
{
const int kiNum = 100;
int* ptr = const_cast<int*>(&kiNum);
*ptr = 200;
cout<<"kiNum: "<<kiNum; // The value still prints 100 on the console??
return 0;
}
output:
kiNum = 100
In the above code snippet , i am trying to change the value of a const integer, after const_cast and then change the value at the address, but the console still prints the old value (i am using visual studio 2012)
Writing to something which is defined as const is undefined (assuming you cast away the const of course).
http://en.cppreference.com/w/cpp/language/const_cast
It's a pretty accurate website. If you have issues with a language feature its always worth looking up there IMHO.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.
This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 8 years ago.
I have a piece of program as shown below:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int i = 100;
cout<<"const i::"<<i<<endl;
const_cast<int&>(i) = 200;
cout<<"const i after cast::"<<i<<endl;
return EXIT_SUCCESS;
}
But the value of i is still 100. Aren't const_cast supposed to change the value of i?
Constant data is, by its very definition, constant, and attempting to change constant data leads to undefined behavior.