P
pemo
Decided to re-post this as a new /thread/.
My comments/question below came up via the 'Seg fault, please help!' post
recently made by BT.
BT wrote:
<snip>
When I first looked at the code above, I initially 'mentally parsed' it as:
'Wow, he's declaring a variable as a parameter in a function call!'.
But, I then saw that this was a function prototype living in non
file-scope, i.e., not placed outside of any function definitions, but within
one.
It's quite unusal to see this, and I never do such a thing - but I think
that's more through habit, than reason, and maybe there's quite some sense
behind using this to restrict scope - if the scope of the prototype is
restricted to the block/function in which it is declared [which the std
seems to suggest is the case?], and, given that we don't yet have nested
functions in std c, it could be quite useful.
So, irrespective of what the current std says: time to hit the compiler and
see what it/they think! Here's some code then...
#include <stdio.h>
int main(void)
{
void func2(void);
{
void func1(void);
func1();
}
func2();
// See below - problems here?: not happy about func1().
//
func1();
return 0;;
}
void func1(void)
{
// See below - problems here?: not happy about func2().
//
func2();
return;
}
void func2(void)
{
return;
}
---
So, with a few different compilers ...
gcc [v4.0.2]:
[Warning] implicit declaration of func-1/2 (2 from func1(), 1 from main)
[Error] incompatible declaration of func-1/2 (2 from func1(), 1 from
main)
---
bcc 5 [v5.5] [Borland CBuilder]:
[Error] type mismatch of func-1/2
---
lcc [v3.8]
Warnings only
---
MSVC6 and MSVC7:
Compiles ok.
So, the compilers differ rather a lot in their opinions about this!
What do others think? Should there be warnings/errors? If 'yes' to either,
should 'style' [as this is often thought of as being 'bad style'] be
re-thought - seems to me that it could be a reasonable habit to adopt.
P.S. Would be interesting to hear what other compilers make of it too!
My comments/question below came up via the 'Seg fault, please help!' post
recently made by BT.
BT wrote:
int main()
{
int DATASIZE = MAXSIZE;
int *narray;
void getdata(int *d, int size);
int largest(int *d, int size);
getdata(narray, DATASIZE);
exit(0);
}
<snip>
When I first looked at the code above, I initially 'mentally parsed' it as:
'Wow, he's declaring a variable as a parameter in a function call!'.
But, I then saw that this was a function prototype living in non
file-scope, i.e., not placed outside of any function definitions, but within
one.
It's quite unusal to see this, and I never do such a thing - but I think
that's more through habit, than reason, and maybe there's quite some sense
behind using this to restrict scope - if the scope of the prototype is
restricted to the block/function in which it is declared [which the std
seems to suggest is the case?], and, given that we don't yet have nested
functions in std c, it could be quite useful.
So, irrespective of what the current std says: time to hit the compiler and
see what it/they think! Here's some code then...
#include <stdio.h>
int main(void)
{
void func2(void);
{
void func1(void);
func1();
}
func2();
// See below - problems here?: not happy about func1().
//
func1();
return 0;;
}
void func1(void)
{
// See below - problems here?: not happy about func2().
//
func2();
return;
}
void func2(void)
{
return;
}
---
So, with a few different compilers ...
gcc [v4.0.2]:
[Warning] implicit declaration of func-1/2 (2 from func1(), 1 from main)
[Error] incompatible declaration of func-1/2 (2 from func1(), 1 from
main)
---
bcc 5 [v5.5] [Borland CBuilder]:
[Error] type mismatch of func-1/2
---
lcc [v3.8]
Warnings only
---
MSVC6 and MSVC7:
Compiles ok.
So, the compilers differ rather a lot in their opinions about this!
What do others think? Should there be warnings/errors? If 'yes' to either,
should 'style' [as this is often thought of as being 'bad style'] be
re-thought - seems to me that it could be a reasonable habit to adopt.
P.S. Would be interesting to hear what other compilers make of it too!