syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 9 : Data Usage

PART 4 : USAGE

CHAPTER 9 : Data Usage
9.1 Declarations
9.2 Using floating point numbers
9.3 Using 'typedef'
9.4 Using global data
9.5 Using Structures
9.6 Using Unions
9.7 Using Arrays
9.8 Using Pointers
9.9 Using bit structures
9.10 Using Constants
9.11 Using 'static' declarations
9.12 Initializing variables
9.13 Summary

<--Prev page | Next page -->

 

9.5  Using Structures

Structures are one of the most powerful tools in C for organizing data, provided that they are used sensibly and carefully.

9.5.1  Keep them cohesive

Collecting data in a structure gives a message: "The data within this structure is highly cohesive." That is, the data is collected using a common rule, typically that the data collectively describes a single complex object:

 

struct BLOCK
{
    unsigned int    Weight;
    unsigned int    Density;
};

 

Note that the description need not be complete in every detail, although it should contain all of the information about that object that will be needed.

9.5.2  Nesting structures

When data naturally breaks down into sub-items, then it is logical to nest the information also:

 

struct BLOCK
{
    unsigned int    Weight;
    unsigned int    Density;
    struct
    {
        unsigned int    Width;
        unsigned int    Depth;
        unsigned int    Height;
    } Size;
};

---------------------------------------------

Nesting shows the clear relationship between the structures, whilst also showing their individual identities. This can, however, make the overall declaration more complex, especially if there are several levels of nesting. A simplification is to separate out the nested structures:

 

struct DIMS
{
    unsigned int    Width;
    unsigned int    Depth;
    unsigned int    Height;
};

struct BRICK
{
    unsigned int    Color;
    unsigned int    Density;
    struct DIMS     Size;
};

 

A common alternative to nesting structures is nesting pointers to structures. It can be confusing if you have a mixture of nested structures and pointers:

 

Book->Chapter.Page->Paragraph.Sentence->Word

 

A simpler approach is to stick to one type of nesting. Pointers are more flexible, although they need more initialization:

 

Book->Chapter->Page->Paragraph->Sentence->Word

 

9.5.3  Beware large structures

There is a danger with large structures as, when they are passed as parameters, only a small part may be used by any one function. A bug in the called function could cause corruption of the other structure data, which may not become apparent until some considerable time later (this type of problem is very difficult to fix!). Such 'tramp data' can be minimized by nesting the structure, as above, and only passing the relevant parts:

 

BrickVol = CalcVolume( &Brick.Size );

 

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-2015
Massive Content -- Maximum Speed