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;
Related
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.
This question already has answers here:
What does "new int(100)" do?
(4 answers)
Closed 1 year ago.
I came across two similar statements but could not exactly find the diference between them. The statements are:
int *p = new int(75);
int *p = new int[75];
Can anyone help me in knowing the difference between the above two statements.
The first returns a pointer to the integer with value of 75, but the second returns a pointer to the array of 75 integers, which don't have a value.
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:
sizeof an array passed as function argument [duplicate]
(3 answers)
Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?
(10 answers)
Closed 8 years ago.
int size(int arr1[])
{
int size1=sizeof(arr1)/sizeof(int);
cout<<size1<<endl;
return size1;
}
void main()
{
int b[]={1,2,3,4,5};
int size2 = size(b);
cout<<size2<<endl;
for (int i=0;i<size2;i++)
{
cout<<b[i];
}
}
I have put the b[] function into size() and check the size then return value.
however, it just return 1 as the answer.
Can anyone please help me to solve this.
A beginner of C++
sizeof(arr1) in the function returns the size of a pointer, not of the whole array.
That´s just how the language is.
You´ve to determine the array size without sizeof:
Either pass a second parameter with the number, or fill the array in a way
you can find the end because a certain value is there (and nowhere else)
This question already has an answer here:
What is the meaning of colon (:) operator in "uint isWidget : 1;" in Qt? [duplicate]
(1 answer)
Closed 9 years ago.
can anyone tell me, what is happening in this code. I tried to search a lot of places but couldn't understand what exactly is the commented part of the code doing.
#include<stdio.h>
struct XYZ {
//int a:6; this one.
char s;
}structure;
int main() {
printf("%lu",sizeof(structure));
return 0;
}
I am getting the output as 4.
That line is commented out. It doesn't do anything.
If it wasn't commented out, it would mean that the size of int a is limited to 6 bits only. It's useful for bit-fields inside of a structure.