20_Limes
20_Limes

Reputation: 45

Function has parameter of type `int`, but gets passed an argument of type `int*`

I have a question about terminology. Consider a unary function. I read in this thread that the function's parameter is the 'variable' used in its declaration, and the 'argument' is the actual value of the variable one passes to the function when calling it. However, consider this simple function:

void f(int *p){};

It has one parameter *p of type int. However, when calling the function I can do

v = 25;
int* w = &v;
f(w);

I.e., I pass an argument of type int* to the function, which means I'm passing an argument of a different type than the parameter.

I'm thinking of a function as a map f:A->B whose domain A determines the type of both parameter and argument, i.e. they should be equal. What is wrong with my reasoning?

Upvotes: 1

Views: 1168

Answers (2)

Silvio Mayolo
Silvio Mayolo

Reputation: 70287

Your function has one parameter p of type int*. You pass it one argument w of type int*. The placement of the * causes some awkwardness at the syntactic level, but for type checking purposes you should regard it as part of the type.

Upvotes: 3

M.M
M.M

Reputation: 141618

*int is a syntax error, not a type. int * is a type .

The name of the parameter is p, not *p . The type of the parameter p is int *. In your sample code you pass an argument of type int * for a parameter of type int *, which is fine.

In general, the argument type can differ from the parameter type, iff it is permitted to initialize a variable of the parameter type with that argument. For example you can pass 5 (an int) to a function whose parameter type is long or float .

Upvotes: 3

Related Questions