no need to precede function and array names with &

L

lovecreatesbea...

K&R 2, sec. 5.11 says that no need to precede function and array names
with address-of operators &, why?
 
G

Guest

Chris said:
Presumably because they though it was worth saying, even though it's
not actually true.

When can an alternative which does not involve the & operator not be
found? (It doesn't have to be a better alternative, just an
alternative.)
 
C

Chris Dollin

Harald said:
When can an alternative which does not involve the & operator not be
found? (It doesn't have to be a better alternative, just an
alternative.)

I don't understand the question (too many `not`s, I don't know whether
to cancel or emphasise). So hoping this strikes appropriately:

* you don't need to preceed a function name with an & to get its
address: the name will decay to that address in value context anyway.

* you don't need to preceed an array name with an & to get the address
of its first element: the name will decay to that address in value
context anyway.

* however, if you want the /address of the array/, as opposed to the
address of its first element, you have to use the & operator to
do so. (This is why I said "not actually true" above.)

As to why K&R thought it worth saying, presumably it's both worth
knowing and not obvious.
 
G

Guest

Chris said:
I don't understand the question (too many `not`s, I don't know whether
to cancel or emphasise). So hoping this strikes appropriately:

Sorry. What I meant is that I believe there is always an alternative
without the & operator, and I asked for a counterexample.
* however, if you want the /address of the array/, as opposed to the
address of its first element, you have to use the & operator to
do so. (This is why I said "not actually true" above.)

You don't. You can use a cast instead.

int main(void) {
int a[2];
int (*pa)[2] = (int (*)[2]) a;
}

And yes, & would be better, that's why I wrote the second sentence in
my previous message.
 
C

Chris Dollin

Harald said:
Sorry. What I meant is that I believe there is always an alternative
without the & operator, and I asked for a counterexample.

Oh, I see.
You don't. You can use a cast instead.

Oh bother, you're right. But:

You can use a cast to do all sorts of thoroughly unsafe things;
I wouldn't like to use one when it was both completely unnecessary
and dangerous.
int main(void) {
int a[2];
int (*pa)[2] = (int (*)[2]) a;
}

And yes, & would be better, that's why I wrote the second sentence in
my previous message.

Indeed, & would be /lots/ better.
 
G

Guest

Harald said:
Chris said:
I don't understand the question (too many `not`s, I don't know whether
to cancel or emphasise). So hoping this strikes appropriately:

Sorry. What I meant is that I believe there is always an alternative
without the & operator, and I asked for a counterexample.
* however, if you want the /address of the array/, as opposed to the
address of its first element, you have to use the & operator to
do so. (This is why I said "not actually true" above.)

You don't. You can use a cast instead.

int main(void) {
int a[2];
int (*pa)[2] = (int (*)[2]) a;
}

And yes, & would be better, that's why I wrote the second sentence in
my previous message.
You could also have done:
FILE *f = (FILE*)a;

That does not mean it is correct, or will "work".
Don't cast, unless you absolutly know why it is ok to do
that cast.
 
G

Guest

Nils said:
Harald said:
You don't. You can use a cast instead.

int main(void) {
int a[2];
int (*pa)[2] = (int (*)[2]) a;
}

And yes, & would be better, that's why I wrote the second sentence in
my previous message.
You could also have done:
FILE *f = (FILE*)a;

Yes, casts at times let you get away with broken code. You have not
explained how my code is broken. If my code is not broken, the fact
that it is far more fragile is not really relevant, considering I
already admitted it was bad style.
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top