Function confusion, whats the diff here?
Author |
Message |
riveryu
|
Posted: Sun May 16, 2010 10:45 pm Post subject: Function confusion, whats the diff here? |
|
|
What is the difference in the following declarations if there is any?
C: |
int *f1(int in);
int *f1(int in){
/*...some code...*/
}
int* f2(int in);
int* f2(int in){
/*...some code...*/
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sun May 16, 2010 10:57 pm Post subject: RE:Function confusion, whats the diff here? |
|
|
There is no non-superficial difference between the two syntaxes. |
|
|
|
|
|
Tony
|
Posted: Sun May 16, 2010 11:10 pm Post subject: RE:Function confusion, whats the diff here? |
|
|
to illustrate the point, white-space is optional. The difference is along the lines of:
code: |
f1 = 1 + 1;
f2 = 1+1;
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
DemonWasp
|
Posted: Mon May 17, 2010 8:29 am Post subject: RE:Function confusion, whats the diff here? |
|
|
The only thing worth noting about this example is that * is right-associative, meaning that if you have this variable declaration list:
then a is a pointer to an integer and b is an integer (NOT a pointer).
This doesn't make any difference in the example listed. |
|
|
|
|
|
rar
|
Posted: Tue May 18, 2010 9:41 am Post subject: RE:Function confusion, whats the diff here? |
|
|
So if you declared:
code: |
int* a, b;
int *c, d;
|
then a, b, and c would be pointers but d would not? |
|
|
|
|
|
DemonWasp
|
Posted: Tue May 18, 2010 10:06 am Post subject: RE:Function confusion, whats the diff here? |
|
|
No. You would have A and C are pointers, but B and D are just ints. The * modifier is right-associative.
If you want A, B, C to be pointers and D to be an integer, then you want:
|
|
|
|
|
|
SNIPERDUDE
|
Posted: Tue May 18, 2010 1:26 pm Post subject: RE:Function confusion, whats the diff here? |
|
|
right-associative = modifies (associated with) whatever is immediately to the right. Anything farther than the comma ( , ) is not associated with the pointer declaration ( * ).
Don't know C, so I may have mixed up a few words, but that is what I understand they are trying to say. |
|
|
|
|
|
DemonWasp
|
Posted: Tue May 18, 2010 1:37 pm Post subject: RE:Function confusion, whats the diff here? |
|
|
Exactly. IF the * modifier was left-associative, then it would modify whatever is to the left of it - the "int" keyword, which would make the following declare A, B, C as pointers to integers:
However, this is NOT how it works. That code would be better formatted as the following, which makes its intent clear:
...and which will also actually make B and C pointers.
I seem to recall there being a reason for this oddity in syntax, but I can't remember it offhand. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
chrisbrown
|
Posted: Tue May 18, 2010 2:03 pm Post subject: Re: RE:Function confusion, whats the diff here? |
|
|
DemonWasp @ Tue May 18, 2010 1:37 pm wrote: I seem to recall there being a reason for this oddity in syntax, but I can't remember it offhand.
One of my CS profs said it was just a way to shorten code by allowing you to declare two (related, but different) variable types in one expression, as opposed to using two lines.
IIRC, the language was being developed at a time when entering characters was costly (due to something like punch cards or simplistic keyboards, I can't remember exactly what), so the designers tried to minimize the number of required characters to produce a working program. |
|
|
|
|
|
rar
|
Posted: Thu May 20, 2010 11:08 am Post subject: Re: RE:Function confusion, whats the diff here? |
|
|
DemonWasp @ Tue May 18, 2010 10:06 am wrote: No. You would have A and C are pointers, but B and D are just ints. The * modifier is right-associative.
If you want A, B, C to be pointers and D to be an integer, then you want:
Oh ok so the modifier itself is right-associative. So my previous belief that the spaces don't have any affect is still true.
My mistake, thanks. |
|
|
|
|
|
|
|