473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global struct declarations

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

Nov 14 '05
20 12420
Mark A. Odell wrote:
Elliot Marks <em****@email.n et> wrote in news:3F******** ******@email.ne t:

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5189
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....
5
8431
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;
10
2644
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...
19
2644
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;
6
2692
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)
3
2998
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...
20
2129
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
6
1783
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;
160
5921
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;
0
9645
marktang
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...
0
9480
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,...
0
10151
jinu1996
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...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
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...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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
3
2879
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.