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(stru ct 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(stru ct mystruct foo)
{
int sum = foo.a + foo.b;
return sum;
}
This code doesn't compile if the struct
declaration is moved inside main(). 20 12418
"Frane Roje" <frane.roje(d*e lete*)@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 ->
--
"Frane Roje" <frane.roje(d*e lete*)@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
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
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****@embedde dfw.com> wrote in message
news:Xn******** *************** *********@130.1 33.1.4... "Frane Roje" <frane.roje(d*e lete*)@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 -> --
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.n et> wrote in message
news:3F******** ******@email.ne t... 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(stru ct 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(stru ct mystruct foo) { int sum = foo.a + foo.b; return sum; }
This code doesn't compile if the struct declaration is moved inside main().
Elliot Marks <em****@email.n et> 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(stru ct 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(stru ct 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*******@hotm ail.com)
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(stru ct 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(stru ct 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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
"Frane Roje" <frane.roje(d*e lete*)@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 ->
--
[top-posting fixed] "Elliot Marks" <em****@email.n et> wrote in message news:3F******* *******@email.n et... 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**********@l s219.htnet.hr>
Frane Roje <frane.roje(d*e lete*)@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(stru ct 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(stru ct 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bryan Parkoff |
last post by:
….I would like to know which is the best optimization to use global
variable or global struct. I always tell C/C++ Compiler to turn on
optimization.
….I use underscore between first name and second name for better
readable. After optimization, global variables might be misaligned
because each global variables must be converted to 32 bits, but I do
see that C/C++ Compiler do padding between variables. Struct does the
same to do padding....
|
by: anonymous |
last post by:
I have a Lex file containing definitions of 2 structures like:
%{
struct a {...};
struct b
{
struct a i;
...
};
struct a x;
|
by: Kleenex |
last post by:
Reason: I am working on an embedded project which has very limited
memory (under 512 bytes, 60 or so of which is stack space), which
translates into limited stack space. In order to save on stack space, I
tried to only use parameters and stack space for things which are truely
temporary. Instead of passing a pointer to a data structure which should
always be populated with data, I have the data structure declared as a
global variable and...
|
by: Russell Shaw |
last post by:
Hi,
I have two structs in a header file, and they reference each other,
causing a compile error. Is there a standard way to deal with this?
typedef struct {
...
RtAction *actions;
} RtWidget;
|
by: S.Tobias |
last post by:
I'm trying to understand how structure type completion works.
# A structure or union type of unknown
# content (as described in 6.7.2.3) is an incomplete type. It
# is completed, for all declarations of that type, by
^^^
# declaring the same structure or union tag with its defining
# content later in the same scope.
^^^^^
(6.2.5#23)
| |
by: Erialc Berts |
last post by:
Forgive me for being a C# newbie.
I have read all of the newsgroup posts, MSDN articles, and other websites on
interop and marshalling to send and receive a C struct as a parameter or
return value in a C function call from C#.
My situation is slightly different, because the C struct I have is global.
I have tried different approaches/techniques (string, String, std::string,
IntPtr, Marshal, class, etc.), but none work. I was given a...
|
by: ma0001yu |
last post by:
Hi, all. I feel confuse about below struct definition:
typedef struct TAG
{
comments....
};
What my confusion is: is typedef extra??why we not just use
|
by: CptDondo |
last post by:
I'm trying to figure out some code that uses structures, structures, and
more structures...
It's a bit of rat's nest, and I'm having some trouble sorting it all out.
The authors use a lot of the following 'declarations' in the various .h
files:
struct document;
struct document_view;
|
by: DiAvOl |
last post by:
Hello everyone,
Please take a look at the following code:
#include <stdio.h>
typedef struct person {
char name;
int age;
} Person;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |