This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Expression does not evaluate to a constant
(1 answer)
Closed 6 months ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
In order to prepare an array, I wrote code below.
#include <iostream>
using namespace std;
int main(){
const double a=0.1;
const double b=0.01;
const int num=a/b;
double array[num];
return 0;
}
Then, I got an error:
expression must have a constant value.
I don't know how to avoid this error. I searched on the internet, but I couldn't find the solution.
Related
This question already has answers here:
Understand structured binding in C++17 by analogy
(1 answer)
What are the new features in C++17?
(1 answer)
Structured binding and tie()
(2 answers)
Closed 4 years ago.
I read a piece of code from a book but I do not really understand the grammar,
const auto& [local_min, local_max] = minmax(A[i], A[i+1]);
where A is vector of int and local_min and local_max are int.
I know minmax returns a pair, but what does the square brackets do, i.e. [local_min, local_max]? I guess it is not for arrays here.
Thanks.
This question already has answers here:
Address of pointer
(5 answers)
C: Why do pointer and &pointer have different values?
(4 answers)
Closed 5 years ago.
Here p is int type of pointer variable.In my code as per I understand output will be same for two line.But it doesn't.So I want to konw why it doesn.t?
#include<iostream>
using namespace std;
int main(){
int* p;
cout<<p<<endl; //0x41c2de
cout<<&p<<endl; //0x28ff0c
}
Question: Why output of two line is not same??
This question already has answers here:
"" + something in C++
(3 answers)
Closed 5 years ago.
I am having trouble , in understanding this program please help:
#include <iostream>
using namespace std;
int main(){
const char* s = 5+"hellow world";
cout<<s;
return 0;
}
It is correct and gives following output
In third line of your code, an anonymous character array is created by the compiler. When you add 5 to the c-string, it performs pointer arithmetic and moves the pointer 5 ahead to the string. Hence, it skips the 5 character from the c-string and only stores other characters from the array into the s.
This question already has answers here:
Difference between int main() and int main(void)?
(10 answers)
Closed 6 years ago.
Can anyone help me with what is different between int main() and int main(void)?
Using (void) as the single parameter in a C++ function is exactly equivalent to using ().
Although, stylistically (void) is to be discouraged.
In C++, there is no difference. The difference only arises in C, where not explicitly mentioning void in the parameter list allows the function to be called with any number of parameters, while the second version only allows exactly 0 parameters.
This question already has answers here:
Usefulness of const (C++)
(6 answers)
Closed 6 years ago.
Hi guys I'm new with C++, whats the difference between using const int n=1 and int n=1 , I dont understant what does const do. Can you give me examples or something?
With const int n=1 you cannot modify the value of n in your code, for example if you try to do n= 4 then it would cause an error,
but with simple int n=1; you can always modify the value of n in your code like this
n= 4;
n=7;