By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

global struct declarations

P: n/a
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().

Nov 14 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
news:bt**********@ls219.htnet.hr:
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?

--
- Mark ->
--
Nov 14 '05 #2

P: n/a
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in message
news:bt**********@ls219.htnet.hr...
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
--

---------------------------
Time to get it done!

Remove (d*elete*) to reply
Nov 14 '05 #3

P: n/a
And 1 more thing
int sum = structfunc(bar);

This code is C++ maybe that is why the code won't compile

--

---------------------------
Time to get it done!

Remove (d*elete*) to reply
Nov 14 '05 #4

P: n/a
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.

--

---------------------------
Time to get it done!

Remove (d*elete*) to reply

"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
news:bt**********@ls219.htnet.hr:
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?

--
- Mark ->
--

Nov 14 '05 #5

P: n/a
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.

--

---------------------------
Time to get it done!

Remove (d*elete*) to reply

"Elliot Marks" <em****@email.net> wrote in message
news:3F**************@email.net...
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().

Nov 14 '05 #6

P: n/a
Elliot Marks <em****@email.net> wrote:
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'.

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #7

P: n/a
Elliot Marks wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

P: n/a
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
news:bt**********@ls219.htnet.hr:
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


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.

--
- Mark ->
--
Nov 14 '05 #9

P: n/a
[top-posting fixed]
"Elliot Marks" <em****@email.net> wrote in message
news:3F**************@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 <bt**********@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 (4039.22'N, 11150.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.
Nov 14 '05 #10

P: n/a
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
news:bt**********@ls219.htnet.hr:
And 1 more thing
int sum = structfunc(bar);

This code is C++ maybe that is why the code won't compile


Well, if this line came before any code then it would be legal C too.

--
- Mark ->
--
Nov 14 '05 #11

P: n/a
On Wed, 7 Jan 2004 19:50:16 +0100, "Frane Roje"
<frane.roje(d*elete*)@st.hinet.hr> wrote:
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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #12

P: n/a
"Mark A. Odell" <no****@embeddedfw.com> writes:
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
news:bt**********@ls219.htnet.hr:
And 1 more thing
int sum = structfunc(bar);

This code is C++ maybe that is why the code won't compile


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #13

P: n/a
On 7 Jan 2004 19:40:58 GMT, Chris Torek <no****@torek.net> wrote in
comp.lang.c:
[top-posting fixed]
"Elliot Marks" <em****@email.net> wrote in message
news:3F**************@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!)


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #14

P: n/a
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.

"Mark A. Odell" <no****@embeddedfw.com> wrote in message
"Frane Roje" <frane.roje(d*elete*)@st.hinet.hr> wrote in
.... snip ...
See how your top posting destroyed the thread?


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #15

P: n/a
Keith Thompson wrote:
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

Nov 14 '05 #16

P: n/a
Elliot Marks <em****@email.net> wrote in news:3F**************@email.net:
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.


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

--
- Mark ->
--
Nov 14 '05 #17

P: n/a
Mark A. Odell wrote:
Elliot Marks <em****@email.net> wrote in news:3F**************@email.net:

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.

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

Nov 14 '05 #18

P: n/a
Elliot Marks <em****@email.net> writes:
Keith Thompson wrote:
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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #19

P: n/a
Keith Thompson wrote:
Elliot Marks <em****@email.net> writes:
Keith Thompson wrote:
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.

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.
Nov 14 '05 #20

P: n/a
Mark A. Odell wrote:
Elliot Marks <em****@email.net> wrote in news:3F**************@email.net:

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.


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


www.comeaucomputing.com

(I have nothing to do with these people FYI)

--
Thomas.

Nov 14 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.