ProgrammerGuide.Net | Step by Step Programmer Guide

C-Structures & Unions

Structures & Unions,

INTRODUCTION
Arrays allow for a named collection of identical objects. This is suitable for a number of tasks, but isn’t really very flexible. Most real data objects are complicated things with an inherent structure that does not fit well on to array style storage. Let’s use a concrete example.

Imagine that the job is something to do with a typesetting package. In this system, the individual characters have not only their character values but also some additional attributes like font and point size. The font doesn’t affect the character as such, but only the way that it is displayed: this is the normal font, this is in italics and this is in bold font. Point size is similar. It describes the size of the characters when they are printed. For example, the point size of this text increases now. It goes back again now. If our characters have three independent attributes, how can they be represented in a single object?

With C it’s easy. First work out how to represent the individual attributes in the basic types. Let’s assume that we can still store the character itself in a char, that the font can be encoded into a short (1 for regular, 2 italic, 3 bold etc.) and that the point size will also fit a short. These are all quite reasonable assumptions. Most systems only support a few tens of fonts even if they are very sophisticated, and point sizes are normally in the range 6 to the small hundreds. Below 6 is almost invisible, above 50 is bigger than the biggest newspaper banner headlines. So we have a char and two shorts that are to be treated as a single entity. Here’s how to declare it in C.
struct wp_char{
char wp_cval;
short wp_font;
short wp_psize;
};
That effectively declares a new type of object which can be used in your program. The whole thing is introduced by the struct keyword, which is followed by an optional identifier known as the tag, wp_char in this case. The tag only serves the purpose of giving a name to this type of structure and allows us to refer to the type later on. After a declaration like the one just seen, the tag can be used like this:
struct wp_char x, y;
That defines two variables called x and y just as it would have done if the definition had been
int x, y;
but of course in the first example the variables are of type struct wp_char, and in the second their type is int. The tag is a name for the new type that we have introduced.  

It’s worth remembering that structure tags can safely be used as ordinary identifiers as well. They only mean something special when they are preceded by the keyword struct. It is quite common to see a structured object being defined with the same name as its structure tag.
struct wp_char wp_char;
That defines a variable called wp_char of type struct wp_char. This is described by saying that structure tags have their own ‘name space’ and cannot collide with other names. We’ll investigate tags some more in the discussion of ‘incomplete types’.

Variables can also be defined immediately following a structure declaration.
struct wp_char{
char wp_cval;
short wp_font;
short wp_psize;
}v1;
struct wp_char v2;
We now have two variables, v1 and v2. If all the necessary objects are defined at the end of the structure declaration, the way that v1 was, then the tag becomes unneccessary (except if it is needed later for use with sizeof and in casts) and is often not present.

The two variables are structured objects, each containing three separate members called wp_cval, wp_font and wp_psize. To access the individual members of the structures, the ‘dot’ operator is used:
v1.wp_cval = ‘x’;
v1.wp_font = 1;
v1.wp_psize = 10;

v2 = v1;

The individual members of v1 are initialized to suitable values, then the whole of v1 is copied into v2 in an assignment.

In fact the only operation permitted on whole structures is assignment: they can be assigned to each other, passed as arguments to functions and returned by functions. However, it is not a very efficient operation to copy structures and most programs avoid structure copying by manipulating pointers to structures instead. It is generally quicker to copy pointers around than structures.

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?