Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > no need to precede function and array names with &

Reply
Thread Tools

no need to precede function and array names with &

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      09-22-2006
K&R 2, sec. 5.11 says that no need to precede function and array names
with address-of operators &, why?

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      09-22-2006
(E-Mail Removed) wrote:

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


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

--
Chris "2 out of 3 isn't bad" Dollin
The shortcuts are all full of people using them.

 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      09-22-2006
Chris Dollin wrote:
> (E-Mail Removed) wrote:
>
> > K&R 2, sec. 5.11 says that no need to precede function and array names
> > with address-of operators &, why?

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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      09-22-2006
Harald van Dijk wrote:

> Chris Dollin wrote:
>> (E-Mail Removed) wrote:
>>
>> > K&R 2, sec. 5.11 says that no need to precede function and array names
>> > with address-of operators &, why?

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


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.

--
Chris "falling further in" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      09-22-2006
Chris Dollin wrote:
> Harald van Dijk wrote:
> > Chris Dollin wrote:
> >> (E-Mail Removed) wrote:
> >>
> >> > K&R 2, sec. 5.11 says that no need to precede function and array names
> >> > with address-of operators &, why?
> >>
> >> 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.)

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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      09-22-2006
Harald van Dijk wrote:

> Chris Dollin wrote:
>> Harald van Dijk wrote:
>> > Chris Dollin wrote:
>> >> (E-Mail Removed) wrote:
>> >>
>> >> > K&R 2, sec. 5.11 says that no need to precede function and array names
>> >> > with address-of operators &, why?
>> >>
>> >> 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.)

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


Oh, I see.

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


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.

--
Chris "falling further in" Dollin
"Who are you? What do you want?" /Babylon 5/

 
Reply With Quote
 
=?UTF-8?B?Ik5pbHMgTy4gU2Vsw6VzZGFsIg==?=
Guest
Posts: n/a
 
      09-22-2006
Harald van Dijk wrote:
> Chris Dollin wrote:
>> Harald van Dijk wrote:
>>> Chris Dollin wrote:
>>>> (E-Mail Removed) wrote:
>>>>
>>>>> K&R 2, sec. 5.11 says that no need to precede function and array names
>>>>> with address-of operators &, why?
>>>> 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.)

>> 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.
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      09-22-2006
Nils O. Selåsdal wrote:
> Harald van Dijk wrote:
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
member function names identical to class names Ares Lagae C++ 8 09-24-2004 11:23 AM
'Imports' statements must precede any declarations David Lozzi ASP .Net 2 08-09-2004 06:23 PM
logical puzzle: how to generate reasonable archive file names fromfile and directory names fBechmann Python 0 06-10-2004 07:13 AM



Advertisments