+ 2

Is there a way to find how many elements are inside a array

int z; cin>>z; int arr[z]; for (int x=0;x < /****/;x++){ //statements; } how can i get how many elements arr has without using z ?

17th Jun 2018, 11:53 AM
Shahil Ahmed
Shahil Ahmed - avatar
11 Respostas
+ 2
int arrsize = sizeof(arr) / sizeof(arr[0]); cout<<arrsize<<endl;
17th Jun 2018, 11:56 AM
$±𝐎â‚č𝔭!𝐹𝓝
$±𝐎â‚č𝔭!𝐹𝓝 - avatar
+ 3
wouldn't the number of elements here just be the value in z?
17th Jun 2018, 11:57 AM
hinanawi
hinanawi - avatar
+ 3
hinanawi a way without using z
17th Jun 2018, 11:59 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
Shahil Ahmed in that case, it's what SagaTheGreat said
17th Jun 2018, 12:00 PM
hinanawi
hinanawi - avatar
+ 2
SagaTheGreat sizeof is a compile time operator so that solution shouldn‘t work, since the size of the array is not know at compile time
17th Jun 2018, 12:04 PM
Max
Max - avatar
+ 2
Max works on here, although it might be undefined behavior
17th Jun 2018, 12:06 PM
hinanawi
hinanawi - avatar
19th Jun 2018, 3:17 AM
MO ELomari
MO ELomari - avatar
+ 1
Max try this, include <iostream> using namespace std; int main() { int z = 0; cin>>z; int arr[z]; cout<< (sizeof(arr)/sizeof(arr[0]))<<endl; return 0; } It is working. sizeof was compile time operator in 89 from 99 it is runtime operator.
17th Jun 2018, 12:15 PM
$±𝐎â‚č𝔭!𝐹𝓝
$±𝐎â‚č𝔭!𝐹𝓝 - avatar
+ 1
SagaTheGreat in c you are right but as far as i know it still is compile time in c++. the standard says the following: "The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, to an enumeration type whose underlying type is not fixed before all its enumerators have been declared, to an array of runtime bound, ..." . note that is says that it should not be applied to an array of runtime bound
17th Jun 2018, 12:31 PM
Max
Max - avatar
+ 1
Max Right, sizeof operator is evaluated runtime for cpp. Then how my trail comment code is working. Do you know ? I know that due to compiler optimization. cin>>z; int arr[z]; cout<< (sizeof(arr)/sizeof(arr[0]))<<endl; so In above code z has some value at runtime. but from int arr[z] compiler knows array is of int type. so at compiler time compile use below expression and it is resolved at run time when z has some value. sizeof(arr) = sizeof(int) * z sizeof(arr[0]) = sizeof(int) now compiler knows sizeof(int).let's say for 32 bit compiler int size is of 4 bytes. then over all cout expression after comiplation will be look like, cout<<(4*z)/4<<endl; hence it is working.
18th Jun 2018, 2:46 PM
$±𝐎â‚č𝔭!𝐹𝓝
$±𝐎â‚č𝔭!𝐹𝓝 - avatar
+ 1
SagaTheGreat i never said that it wouldn‘t work i just said that its not in the standard and that you can therefore not expect it to work in every compiler or even across compiler versions or for more complex code, so one day in the future Shahil Ahmed will try to use your trick for something more complicated and it wont compile or give him weird results
18th Jun 2018, 2:47 PM
Max
Max - avatar