prev up next   top/contents search

comp.lang.c FAQ list · Question 4.10

Q: I have a function

	extern int f(int *);
which accepts a pointer to an int. How can I pass a constant by reference? A call like
doesn't seem to work.

A: In C99, you can use a ``compound literal'':


Prior to C99, you couldn't do this directly; you had to declare a temporary variable, and then pass its address to the function:

	int five = 5;
In C, a function that accepts a pointer to a value (rather than simply accepting the value itself) probably intends to modify the pointed-to value, so it may be a bad idea to pass pointers to constants. [footnote] Indeed, if f is in fact declared as accepting an int *, a diagnostic is required if you attempt to pass it a pointer to a const int. (f could be declared as accepting a const int * if it promises not to modify the pointed-to value.)

See also questions 2.10, 4.8, and 20.1.

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North