global struct declarations

E

Elliot Marks

If a struct or its members are passed
to a function, must it be declared globally?

#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);
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().
 
F

Frane Roje

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.
And your code is strange. It seems that you are using a little bit of C and
C++. Everything looks like C except for the
int sum = structfunc(bar); which is C++ code.
 
M

Mark A. Odell

I've never seen a declaration of struct inside a function.

I do it all the time. If I need to package up some common set of data
inside a function to make it more clear I do. Why should I expose the
struct declaration outside the function?
I believe it's
not common and there is no reason why should anyone declare it inside a
function.

Yes there is, it is no use to other functions.

See how your top posting destroyed the thread?
 
F

Frane Roje

Well we're all here to learn, I'm glad I said what I said 'cause now I know
more!

I use VC7 and i wrote a code with a struct inside main() and it did not show
up in the class view, does it appear in your editor? If I declare a global
struct i does show in the class view.
 
F

Frane Roje

Frane Roje said:
Well we're all here to learn, I'm glad I said what I said 'cause now I know
more!

I use VC7 and i wrote a code with a struct inside main() and it >did not show
up in the class view, does it appear in your editor? If I declare a global
struct i does show in the class view.


I forgot to mention that it does complie and work with a struct inside
function
 
A

Alex Monjushko

Elliot Marks said:
If a struct or its members are passed
to a function, must it be declared globally?

Yes for struct. No for members.
#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);

Unless you are using C99, you cannot intermix declarations
and statements.
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().

Of course it doesn't compile. By declaring 'mystruct' in main,
you can only use it main. It is no longer visible to 'structfunc'.
 
C

Chris Torek

[top-posting fixed]

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!)

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]
Note that this syntax (which does look a lot like C++, as Frane Roje
noted) is new in C99.

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.)
 
C

CBFalconer

Elliot said:
If a struct or its members are passed
to a function, must it be declared globally?

#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);
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().

That is because it is not a declaration, but a type definition.
That definition is not visible outside the source file.
 
M

Mark A. Odell

I forgot to mention that it does complie and work with a struct inside
function

Of course, it's legal :). I never use IDEs except for building and
debugging code. That is I edit with my editor and the ALT-TAB to the IDE
and push the build button, then debug. I use CodeWright (now owned by
Borland) and it does show all structs, local or otherwise.
 
A

Alan Balmer

Well we're all here to learn, I'm glad I said what I said 'cause now I know
more!

I use VC7 and i wrote a code with a struct inside main() and it did not show
up in the class view, does it appear in your editor? If I declare a global
struct i does show in the class view.

It shows up in the class view in Slickedit. However, for code
completion assistance, it will only show up if it's in scope. Which is
as it should be, of course.
 
K

Keith Thompson

Mark A. Odell said:
Well, if this line came before any code then it would be legal C too.

Mixing declarations and statements is also legal in C99, and may be
supported as a language extension by some pre-C99 compilers.

But you should avoid it if you're trying to write portable C90 code.
 
J

Jack Klein

[top-posting fixed]

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!)

Actually the "union" keyword, and arguably the "enum" keyword create
new types as well.
 
C

CBFalconer

Frane Roje wrote: ** and rudely topposted - not fixed **
I use VC7 and i wrote a code with a struct inside main() and it did not show
up in the class view, does it appear in your editor? If I declare a global
struct i does show in the class view.

Why do you insist on topposting after having been shown the
problem? Your answer goes after (or interspersed with) the
material you are quoting, with non-germane junk snipped out.

In addition, C has no classes, class views, editors, or VC7s.
This group deals with the standard language, not C++ nor specific
implementations.
 
E

Elliot Marks

Keith said:
Mixing declarations and statements is also legal in C99, and may be
supported as a language extension by some pre-C99 compilers.

But you should avoid it if you're trying to write portable C90 code.
In the process of learning C I have never used anything but a
C99 compiler, so I had no idea that this was a portability issue.
Thanks for the info.

Elliot
 
M

Mark A. Odell

Elliot Marks said:
In the process of learning C I have never used anything but a
C99 compiler, so I had no idea that this was a portability issue.
Thanks for the info.

Really? Which one (e.g. vendor)? I've yet to come across a C99 compiler.
 
E

Elliot Marks

Mark said:
Really? Which one (e.g. vendor)? I've yet to come across a C99 compiler.

I'm using Digital Mars C. I suppose if you define a "C99
compiler" as one which supports every feature of C99 I have
misspoken. OTOH, I suppose you could define a "C99 compiler" as
one which supports *any* feature new with C99. Let's put it this
way: I've yet to come across any C99 feature that is not
supported by DMC. In any event, let's not start a thread over it. :)

Elliot
 
K

Keith Thompson

Elliot Marks said:
In the process of learning C I have never used anything but a C99
compiler, so I had no idea that this was a portability issue. Thanks
for the info.

There aren't many C99 compilers yet. It's more likely that you're
using a compiler that includes some C99 features as extensions. In
the case of mixing statements and declarations, or supporting "//"
comments, the features could also have been borrowed from C++.

What does the following program print?

#include <stdio.h>
int main()
{
#ifdef __STDC__
printf("__STDC__ = %d\n", __STDC__);
#else
printf("__STDC__ is not defined\n");
#endif
#ifdef __STDC_VERSION__
printf("__STDC_VERSION__ = %ld\n", __STDC_VERSION__);
#else
printf("__STDC_VERSION__ is not defined\n");
#endif
return 0;
}

For a conforming C99 implementation, it should print

__STDC__ = 1
__STDC_VERSION__ = 199901

(Of course, it could print the same thing for a non-conforming C99
implementation; by definition, the standard can place no constraints
on non-conforming implementations.)
 
E

Elliot Marks

Keith said:
There aren't many C99 compilers yet. It's more likely that you're
using a compiler that includes some C99 features as extensions. In
the case of mixing statements and declarations, or supporting "//"
comments, the features could also have been borrowed from C++.

What does the following program print?

#include <stdio.h>
int main()
{
#ifdef __STDC__
printf("__STDC__ = %d\n", __STDC__);
#else
printf("__STDC__ is not defined\n");
#endif
#ifdef __STDC_VERSION__
printf("__STDC_VERSION__ = %ld\n", __STDC_VERSION__);
#else
printf("__STDC_VERSION__ is not defined\n");
#endif
return 0;
}

For a conforming C99 implementation, it should print

__STDC__ = 1
__STDC_VERSION__ = 199901

(Of course, it could print the same thing for a non-conforming C99
implementation; by definition, the standard can place no constraints
on non-conforming implementations.)

It prints:

__STDC__ is not defined
__STDC_VERSION__ = 199901

I'm not sure what all this means, (and if it were not OT I would
ask) but it sounds like you are right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top