0
in C++ why can't we use sizeOf() function int main function
4 Antworten
+ 5
To compare the values, it is necessary to bring them to the same type:
for (int i=0; i < (int)sizeof(nums); i++) {...}
Or use a new variable type:
for (size_t i = 0; i < sizeof(nums); i++) {...}
+ 3
#include <iostream>
using namespace std;
int main() {
int target;
cin >> target;
int nums[] = {2, 7, 11, 15};
int size = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i != j && nums[i] + nums[j] == target) {
cout << i << " " << j << endl;
break;
}
}
}
return 0;
}
+ 2
In C++ it is sizeof, not sizeOf().
There are some important subtleties to know about sizeof in C and C++. Be aware that sizeof is not a function. It is a unary operator [just like a negative sign (-) is a unary operator]. It is evaluated at compile time only. If your code has an expression such as
y = sizeof myFunction(x);
beware that myFunction(x) will not get called at runtime. When it compiles, the compiler merely looks up the data type of myFunction() and substitutes the size of its return data type instead of inserting run-time code to call the function.
+ 1
try with sizeof() instead of sizeOf() 🤔🤔
some people confuse with C++ because they went from Java, that is usual the function express with capital letters.. 😅😅
good luck bro..