function prototype and arguments conversion

M

Mark

Hello

void func(unsigned long);

void func(unsigned long i)
{
return;
}

int main(void)
{
int p = 10, k = -7;
double t = 1.9;

func(p); /* case 1 */
func(k); /* case 2 */
func(t); /* case 3 */

return 0;
}

Am I right, that case 2 and 3 invoke undefined behavior? Back up quote form
the Standard, 6.5.2.2p9: "If the function is defined with a type that is not
compatible with the type (of the expression) pointed to by the expression
that denotes the called function, the behavior is undefined."

If I'm wrong, what does the Standard say about the situations above ?
Thanks.
 
S

Seebs

Hello

void func(unsigned long);

void func(unsigned long i)
{
return;
}

int main(void)
{
int p = 10, k = -7;
double t = 1.9;

func(p); /* case 1 */
func(k); /* case 2 */
func(t); /* case 3 */

return 0;
}

Am I right, that case 2 and 3 invoke undefined behavior?

I don't think so.
Back up quote form
the Standard, 6.5.2.2p9: "If the function is defined with a type that is not
compatible with the type (of the expression) pointed to by the expression
that denotes the called function, the behavior is undefined."

You are reading the wrong chunk of text entirely. Note "pointed to by the
expression that denotes the called function".

The section you're looking at covers function call dereferences.

So, say, if you had 'int x(int i);' and 'int (*y)(long l);', and converted
x to the type of y, then tried to call y, you'd be invoking undefined behavior
because those two function types are not compatible.

You want 6.5.2.2, paragraph 7, which covers the conversion rules for the
arguments.
If I'm wrong, what does the Standard say about the situations above ?
Thanks.

They're converted as though by assignment. All three produce well-defined
behavior. The first calls func with the unsigned long value 10, the second
with the unsigned long value (ULONG_MAX+1)-7, and the third with the unsigned
long value 1.

-s
 
M

Mark

Seebs said:
They're converted as though by assignment. All three produce well-
defined behavior. The first calls func with the unsigned long value
10, the second with the unsigned long value (ULONG_MAX+1)-7, and the
third with the unsigned long value 1.


What part of the Standard spells out about assignment rules (when assigning
signed value to unsigned object, or assigning very large value that can
cause overflow etc. ) ?

Thanks.
 
S

Seebs

What part of the Standard spells out about assignment rules (when assigning
signed value to unsigned object, or assigning very large value that can
cause overflow etc. ) ?

6.5.16.1 covers assignment, 6.3 covers conversions, you combine them and it
all makes sense.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top