Is there any guarantees that the functions which differs only by its names (not parameters and return type also) can't share the same address in C and C++? I don't see anything about it in the standard.
#include <cassert>
void foo() {}
void bar() {}
int main()
{
assert(foo != bar);
}
Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.
Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.
then you can declare multiple int-pointer variables by simply giving the int-pointer type (int *) followed by a comma-separated list of names to use for the variables ( ptr_a, ptr_b ).
Function Pointer Syntaxvoid (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.
The C++11 standard says
5.10 Equality operators
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 (3.9.2).
If you don't have any pointers to the functions, they just might have the same address, but we wouldn't know. If you are comparing pointers to two different functions, they must not compare equal.
One cause for confusion might be that the MSVC compilers are known to combine code for template functions that happen to produce identical machine code for different types (like int
and long
). This is not compliant.
However, this is for functions with different signatures, and not exactly what this question is about.
Yes. C99 6.5.10:6
Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, …
Edit: the rest of the paragraph, since it turns out to have some importance:
both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.
What I take from it:
<
is undefined when you use it wrong.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With