C Programming – Structures and Unions

A structure can be considered as a template used for defining a collection of different types of variables under a single name.
Structures help a programmer in grouping elements of different data types into a single logical unit. This is unlike arrays
which permit a programmer to group only elements of same data type.  Structures are data types by themselves. When you define
a structure or union, you are creating a custom data type.
Common Syntax of Using Structure

The primary use of a struct is for the construction of complex datatypes, but in practice they are sometimes used to circumvent standard C conventions to create a kind of primitive subtyping. For example, common Internet protocols rely on the fact that C compilers insert padding between struct fields in predictable ways;

structure definition:
general format:

struct tag_name
data type member1;
data type member2;

Structures are like arrays except that they allow many variables of different types grouped together under the same name.
For example you can create a structure called “contact” which is made up of a string (that is used to hold the name of the person) and an integer (that is used to hold the telephone number).
Take a look at the example:

Struct contact
char *name;
int number;
float usage;

The above is just a declaration of a type. You must still create a variable of that type to be able to use it.
Here is how you create a variable called person of the type contact:*/

#include <stdio.h>
struct contact
char* name;
unsigned int number;
float usages;
void main()
struct contact person;            //structure variable person created
person.name = "John";            //using . operator you can acces individual elements of structure
person.number = 12345;
person.usages = 10.25;

//For printing again we have to access individually all elements with proper specifiers




Type definitions
Type definitions make it possible to create your own variable types.Once you have typedefined a
unsigned integer as Length,
typedef int Length
makes the name Length a synonym for int. The type Length can be used in declarations, casts, as any native data type,
and declare other variables with its data type. You can similarly typedef a structure too.It is like the storage classes
extern, static, etc. It is adviced to use capitalized names for typedefs, to make them stand out.

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.


Comments (1)
  1. Simonne Minarik says - Posted: October 24, 2012

    Hello. Very nice net site!! Man .. Admirable .. Brilliant .. I’ll bookmark your website and assume the feeds additionally…I’m pleased to attain abundant helpful information here contained by the post. Merit for sharing..

Leave your comment

Your email address will not be published. Required fields are marked *