Re: global struct declarations

From: Chris Torek (nospam_at_torek.net)
Date: 01/07/04


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.


Relevant Pages

  • Re: Problems dereferensing my C structures (custom types)
    ... implementation, at file scope. ... This declaration probably is at file ... _tbl_certificate is not a legal name for that struct. ... (Identifiers beginning with __ or _ and a capital letter are reserved ...
    (comp.lang.c)
  • Re: Porting from visual C to gcc problem
    ... The declaration above defines the identifier 'DNIS_TIMER_CALLBACK' as ... of a structure type named 'struct DNS_TIMER_CB'. ... C is a scoped language, and one of the scopes is "prototype scope". ... int func; ...
    (comp.lang.c.moderated)
  • Re: How to pass a struct to a function
    ... write an expression evaluating to your struct -- the name of a variable ... you haven't declared a type "struct entry". ... about how this declaration is handled, ... on which compiler you're using, but any decent compiler will print ...
    (comp.lang.c)
  • Re: Passing Structure to a function
    ... > int Menu; ... 'struct' keyword above is because otherwise you got compiler errors. ... The reason for the errors is that phonerec hasn't been declared or ... A structure declaration merely says that a structure ...
    (comp.lang.cpp)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)