Error: too few arguments to function 'int FunctionA(float, float)'
I can't seem to fix this, I'm not really sure what to do. I'm still in the process of learning how to use functions and I'd deeply appreciate help.
I'm basically trying to make a calculator, convert a char to an int that works like an int defined by cin and it goes through the functions with two floats to find a result and return to the FunctionA where it started and print the answer.
leads to undefined behaviour because Operator was defined as an array of only one element. Moreover this statement
if (Operator == "Add"){
compares addresses of Operator and string literal "Add". As they have different addresses the expression will be always equal to false though Operator does not contain string "Add".
"As they have different addresses the expression will be always equal to true false"
Not necessarily. With optimisations enabled, a smart compiler will place all identical strings into one location within static memory to save space. If the latter is true, then comparing two strings may always yield true, not false. Though, this may be beyond the scope of the OP's question, but it's relevant.
With optimisations enabled, a smart compiler will place all identical strings into one location within static memory to save space. If the latter is true, then comparing two strings may always yield true, not false. Though, this may be beyond the scope of the OP's question, but it's relevant.
There is no a comparison of two string literals. There is a comparison of the address of Operator and some string literal.
> if (Operator == "Add"){
> compares addresses of Operator and string literal "Add".
> As they have different addresses the expression will be always equal to false
It results in unspecified behaviour.
Tip: compile your code with all warnings enabled.
1 2
constchar Operator[] = "Add" ;
Operator == "Add" ; // warning: comparison with string literal results in unspecified behaviour
No, the result is very well specified and will be equal to false because there is a comparison of the address of the first element of array Operator with the address of the first element of other array that has no name and contains string literal "Add".
I read the standard and it is me who is right not you.
From the C++ Standard
Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address
Yes the result is well-defined. As it follows from the quote the result will be always equalt to false because the both expressions (operands) do not represent the same address.
its not comparing addesses vlad its comparing a "pointer to a memory location" to a string literal, not its memory location
edit: put quotes around pointer..location to show its one thing. other wise it looks like i mean comparing a pointer, a memory location, and a string liiteral
its not comparing addesses vlad its comparing a pointer to a memory location to a string literal, not its memory location
String literals in C++ have type const char [] that is they are arrays. In expressions the same way as other arrays they are converted implicitly to the pointer to the first element
Otherwise if to follow your logic the following code has no sense
It is comparing addresses alright; it is just that the result of the comparison with a string literal is unspecified. This thread goes on and on because vlad from moscow is ignorant about the differences between undefined behaviour, implementation defined behaviour and unspecified behaviour. And he is ignorant about his ignorance.
You are mistaken. I see the difference between undefined behaviour and unspecified behaviour. The standard specified very well the result of the comparision of two pointers of the same type. Read one more the quote I posted.
String literals in C++ have type const char [] that is they are arrays. In expressions the same way as other arrays they are converted implicitly to the pointer to the first element
^that. and also i understand what you wrote below that, but that wasnt i was saying. i get comparing pointers, but i didnt think string literals were converted to char *'s so in my mind it was comparing two different data types, like testing float myfloat(1.4) == bool mybool(false)