473,768 Members | 9,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

no need to precede function and array names with &

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

Sep 22 '06 #1
7 1869
lovecreatesbea. ..@gmail.com 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.

Sep 22 '06 #2
Chris Dollin wrote:
lovecreatesbea. ..@gmail.com 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.)

Sep 22 '06 #3
Harald van Dijk wrote:
Chris Dollin wrote:
>lovecreatesbea ...@gmail.com 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/

Sep 22 '06 #4
Chris Dollin wrote:
Harald van Dijk wrote:
Chris Dollin wrote:
lovecreatesbea. ..@gmail.com 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.

Sep 22 '06 #5
Harald van Dijk wrote:
Chris Dollin wrote:
>Harald van Dijk wrote:
Chris Dollin wrote:
lovecreatesbea ...@gmail.com 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/

Sep 22 '06 #6
Harald van Dijk wrote:
Chris Dollin wrote:
>Harald van Dijk wrote:
>>Chris Dollin wrote:
lovecreatesb ea...@gmail.com 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.
Sep 22 '06 #7
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.

Sep 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1965
by: Phil Powell | last post by:
<?php class Grad { var $dbFormExemptionArray = array(); function Grad ($id = '') { /*---------------------------------------------------------------------------------------------------------------------- Do note that if you are generating arrays that will not have
8
4833
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
27
2488
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to a pointer? #include <stdio.h> /* declares a function which takes an argument that is a...
2
1551
by: newhand | last post by:
If somehow a bunch of function names in string can be passed into an executible, is it possible that for each name call, it will trigger the corresponding function of that name? Of course, this can be done by mapping string function names with functions themselves? Is there a better and direct way? Thanks in advance!
3
5616
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we want to give them the opportunity to run these modules from home as well, we are trying to get the USERNAME/COMPUTERNAME as well, so the students, when they sign up for the modules, they can ONLY run the modules fromhome PC. We found a nice...
1
5126
by: jianxin9 | last post by:
Hi, I have an ajax powered tabs box that has a javascript drop-down search menu in the first tab. When I click on another tab, and go back to the first tab I have to refresh the page to get the information to load. Any suggestions on how I might get around that. The articles tab is the tab where the javascript won't load. Thanks so much for any help you might be able to provide: This is what the tabs code looks like: <ul...
1
2103
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions. In this initial phase of the...
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9963
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9844
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7386
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5284
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3933
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.