Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Function confusion, whats the diff here?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
riveryu




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: 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;
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: 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:

code:
int * a, b;


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




PostPosted: 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




PostPosted: 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:
code:

int *a, *b, *c, d;
SNIPERDUDE




PostPosted: 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




PostPosted: 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:
code:
int* A, B, C;


However, this is NOT how it works. That code would be better formatted as the following, which makes its intent clear:
code:
int *A, *B, *C;


...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
Sponsor
sponsor
chrisbrown




PostPosted: 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




PostPosted: 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:
code:

int *a, *b, *c, d;


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.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: