1

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?

20_Limes
  • 35
  • 5

2 Answers2

3

*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 .

M.M
  • 138,810
  • 21
  • 208
  • 365
3

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.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • Is there a reason why I can write `f(int *p)` in the first place? I just read about the dereference operator `*` and thought I would try to use the 'value a pointer points to' as a function parameter to see whether it works, and it did. Hence my confusion. – 20_Limes May 18 '21 at 00:57
  • C++ doesn't care whether you put spaces in that situation. `int* p`, `int *p`, and `int * p` are all equivalent, as far as your compiler is concerned. Strictly speaking, internally, it's treated as `int *p` in all cases (you can distinguish between them by declaring multiple variables on one line). But that's largely a case for not declaring multiple variables on one line, more than it is a case for writing `int *p`. Personally, I prefer `int* p` because I like to think of `int*` as the type. – Silvio Mayolo May 18 '21 at 00:58
  • I was thinking more along the lines of the last line of this simple example: `int v = 25; int* p = &v; int a = *p`. For me this reads 'as if' `*p` has type integer, and this was what I wanted to pass to my function. I suppose I am mistaken because I had to define `p` in the first place, and `*p` isn't really an 'object'? – 20_Limes May 18 '21 at 01:05
  • If you want to take an integer, you need to declare your function to take `int p`, not `int* p`, then you can call it as `f(*w)`. – Silvio Mayolo May 18 '21 at 01:06
  • Ok, now I get it. – 20_Limes May 18 '21 at 01:11
  • @20_Limes in the code `int *p`, the symbol `*` is not the dereference operator. Symbols have different meanings in declarations than in expressions. (`&`, `[ ]` and `=` are other examples of this). An operator operators on other expression(s) to make an expression. – M.M May 18 '21 at 01:11
  • @M.M Thank you, I see now that it is not the dereference operator. Originally, I tried to extrapolate from this: `int* p = &v; int a = *p` to function declarations, trying both uses of `*` as parameter. I guess my 'problem' is that the IDE didn't complain when I wrote `f(int *p)`, because that looks like the second use in the example above (with dereference operator), but apparently actually means `f(int* p)`. – 20_Limes May 18 '21 at 01:25
  • @20_Limes whitespace is not significant in C, except when it affects tokenization, e.g. `+ + x` differs from `++x` because `++` is a single token; but `int`, `*` and `p` are three different tokens so can have any amount of intervening whitespace, including none. – M.M May 18 '21 at 01:27
  • @M.M That is good to know. It will make it easier for me to remember the differences between the cases above. – 20_Limes May 18 '21 at 01:33