Structures & Unions 
Author Message
 Structures & Unions

Would it be fair to say that larger programs will always use structures and
unions??

Regards

Tim



Tue, 30 Dec 2003 23:34:09 GMT  
 Structures & Unions

Quote:

> Would it be fair to say that larger programs will always use structures and
> unions??

Opinion: Usage of structures and unions is probably independent of program size
(except, possibly, for homework exercises 8-)

Observation: Structures are more frequently used than unions and are commonly
used in even very small programs.

HTH
--
Morris Dovey
West Des Moines, Iowa USA
Available for embedded C contract work



Tue, 30 Dec 2003 23:42:03 GMT  
 Structures & Unions

Quote:

> Would it be fair to say that larger programs will always use
> structures and unions??

Every large program whose code I've read used structures.
Unions are probably less common.
--
"I hope, some day, to learn to read.
 It seems to be even harder than writing."
--Richard Heathfield


Tue, 30 Dec 2003 23:42:48 GMT  
 Structures & Unions

Quote:

> Would it be fair to say that larger programs will always use structures and
> unions??

I would say the odds are pretty good that you will find a structure
somewhere inside a large program, but I hardly ever use a union.

As for saying they "always" will, I can imagine a case where both
structures and unions would not be necessary in a large program.

--
== Eric Gorr ===== http://www.*-*-*.com/ :9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Wed, 31 Dec 2003 00:20:09 GMT  
 Structures & Unions

Quote:
> Would it be fair to say that larger programs will always use structures
> and unions??

Yes, and if only because most standard library implementations use them a
lot ;)

Daniel

--

` { } \ | [ ] '



Wed, 31 Dec 2003 01:33:11 GMT  
 Structures & Unions
Thanks for that Eric.

When would you not use a function or union in a large program??

Tim



Wed, 31 Dec 2003 04:25:59 GMT  
 Structures & Unions

Quote:

> When would you not use a function or union in a large program??

(I think you mean "structure" or union, given the thread.)

When it's not the best way to get something done.  No one sits
down and says, "I want to write this 100000-line program without
using any structures or unions."  No one with any sense, anyhow.
But it is possible that some 100000 line program might not have
any legitimate need for a structure or union, and in that case,
there is no need to use them.

I really doubt that a big program wouldn't find some use for one,
though.  Structures are one of the most important ways, in C, to
perform data hiding and abstraction.  Big programs have more need
for these than little programs, so big programs tend to use them.

What's the reason for the original question, anyway?
--
"Debugging is twice as hard as writing the code in the first place.
 Therefore, if you write the code as cleverly as possible, you are,
 by definition, not smart enough to debug it."
--Brian Kernighan



Wed, 31 Dec 2003 04:30:39 GMT  
 Structures & Unions
I am studying programming in C, as I am just learning I am trying to understand
the nature of the beast!!


Wed, 31 Dec 2003 04:42:03 GMT  
 Structures & Unions


Quote:
>Thanks for that Eric.

>When would you not use a function or union in a large program??

When you don't need to.

You use language features wne you need to use them. So if you need to
create an aggregate type, a struct is perfect for that, and when you
need to create a composite type, a union is good.

i've written 100 line programs that benefitted from the use of
structs. Adn i've written 20000 line ones that never touched them.

The point: there are no size-based rules about when you do and do not
use language features. You use them if you need them.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Wed, 31 Dec 2003 06:21:14 GMT  
 Structures & Unions
Structures are used in programs of all types and sizes.  Unions you will
typically find in programs that do alot of network and hardware programming.

With hardware programming you will often see structures within unions.
There is a special type of structure called a bitfield structure that allows
you to address individual bits by name.  You might create a union containing
an int and a 32 bit bitfield structure.  Then you could store an int that
represents the status of a piece of hardware in one part of the union and
look at the individual bits with the other part of the union, etc. etc.
Features like this make C well suited for network and hardware programming.

I wrote the previous paragraph because when I was learning C it was alot
easier to understand unions when I was given a good practical example for
their use.

Regards,

Kyle Patterson
Senior Systems Programmer/Analyst

Regards,
Kyle Patterson
Senior Systems/Programmer Analyst.


Quote:
> Would it be fair to say that larger programs will always use structures
and
> unions??

> Regards

> Tim



Thu, 01 Jan 2004 06:02:55 GMT  
 Structures & Unions
Kyle Patterson a crit dans le message

Quote:
>Structures are used in programs of all types and sizes.  Unions you
will
>typically find in programs that do alot of network and hardware

programming.

I'm afraid you're wrong.

Quote:
>With hardware programming you will often see structures within
unions.
>There is a special type of structure called a bitfield structure that
allows
>you to address individual bits by name.  You might create a union
containing
>an int and a 32 bit bitfield structure.  Then you could store an int
that
>represents the status of a piece of hardware in one part of the union
and
>look at the individual bits with the other part of the union, etc.
etc.
>Features like this make C well suited for network and hardware

programming.

Not at all. Bitfields are not portable at all, neither are union
initialization. True C programmers use bitwise operators.

Quote:
>I wrote the previous paragraph because when I was learning C it was
alot
>easier to understand unions when I was given a good practical example
for
>their use.

Unions was not design for that at all. Unions are useful for memory
saving in structures, or to pass various parameters in a fixed size
array of char (often used in data exchange between tasks on the *same*
machine).

union message
{
   unsigned char array[32]; /* system feature */
   struct
   {
      int type; /* generic selector */
      union
      {
         struct
         {
            int a;
            float b;
            char *c;
         } type_1;

         struct
         {
            int a;
            int b;
            long c;
         } type_2;

         /* etc. */

      } parameters;
   } msg;

Quote:
}

For safety, better to invoke

   assert (sizeof (struct message) <= 32);

in some test or init code (at last at debug time)

--
-hs-    "boycott Beijing 2008" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkumware.com/htm_cl/index.html
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Thu, 01 Jan 2004 06:43:35 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Packed structures & unions

2. structure, union & enumeration tags

3. Union & Structures

4. Question regarding unions and structures

5. fread w/ Union structures

6. Structures vs. Unions

7. Initialising Union members of Structures

8. Bit-fields, unions and structures...

9. help: unnamed unions within structures

10. Union within structures

11. Alignment of data in a Structure/Union

12. structure union mess

 

 
Powered by phpBB® Forum Software