Re: global struct declarations
From: Chris Torek (nospam_at_torek.net)
Date: 01/07/04
- Next message: Jimmie: "web base download"
- Previous message: Mark A. Odell: "Re: global struct declarations"
- In reply to: Frane Roje: "Re: global struct declarations"
- Next in thread: Jack Klein: "Re: global struct declarations"
- Reply: Jack Klein: "Re: global struct declarations"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Jan 2004 19:40:58 GMT
[top-posting fixed]
>"Elliot Marks" <emarks@email.net> wrote in message
>news:3FFC47CD.3040901@email.net...
>> If a struct or its members are passed
>> to a function, must it be declared globally?
Any type must be declared at a scope such that its declaration is
visible to everyone using it. In this case, if some file(s) are
passing or returning entire structs, those structs must be declared
at file scope.
(Remember that "struct foo" is how you define a new type in C.
Some people like to decorate this with an additional "typedef",
but the typedef does NOT define a new type, just an alias for it.
The "struct" keyword is the one that defines the type!)
In article <bthi1p$9l4$1@ls219.htnet.hr>
Frane Roje <frane.roje(d*elete*)@st.hinet.hr> wrote:
>I've never seen a declaration of struct inside a function. I believe it's
>not common and there is no reason why should anyone declare it inside a
>function.
One declares types (such as new "struct"s) inside functions for
the same reason one declares anything inside functions: to restrict
the scope of the type-names, variables, and so forth.
[Elliot Marks]
>> #include <stdio.h>
>> struct mystruct{
>> int a;
>> int b;
>> };
>> int structfunc(struct mystruct foo);
>>
>> int main(void)
>> {
>> struct mystruct bar;
>> bar.a = 123;
>> bar.b = 456;
>> int sum = structfunc(bar);
Note that this syntax (which does look a lot like C++, as Frane Roje
noted) is new in C99.
>> printf("%d\n", sum);
>>
>> return 0;
>> }
>>
>> int structfunc(struct mystruct foo)
>> {
>> int sum = foo.a + foo.b;
>> return sum;
>> }
>>
>> This code doesn't compile if the struct
>> declaration is moved inside main().
The "struct mystruct" type needs to be in scope for both the
prototype declaration of "structfunc" and for the definition
of "structfunc". You can achieve the former even with a block-scope
definition of the struct by putting the prototype inside main()
as well; but the latter requires "struct mystruct" to occur at
file scope.
Remember, type declarations and definitions have scope, just like
ordinary variable declarations. (But type names do not have linkage,
unlike ordinary variables.)
-- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: forget about it http://web.torek.net/torek/index.html Reading email is like searching for food in the garbage, thanks to spammers.
- Next message: Jimmie: "web base download"
- Previous message: Mark A. Odell: "Re: global struct declarations"
- In reply to: Frane Roje: "Re: global struct declarations"
- Next in thread: Jack Klein: "Re: global struct declarations"
- Reply: Jack Klein: "Re: global struct declarations"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|