Search This Blog

Thursday, 20 October 2011

Arrays and Structures in C!

1 comments

USING ARRAYS AND STRUCTURES!

An array is defined abstractly as a finite ordered set of homogeneous elements.  It is a data type, which is derived from primitive data types in C.  The simplest form of an array is one-dimensional array.  An example for one-dimensional array is a String of characters of size n, where n is the number of characters in the string.

The word ‘finite’ in the above definition indicates that there is a specific number of elements in the array.  The word ‘ordered’ means the elements of the array are arranged so that there is a zeroth, first, second, third and so forth.  By ‘homogeneous’ we mean that all the elements in the array must be of the same type.

Declaring an Array:

Array must be declared first, before we use it in a program.  Declaring an array involves the following details:
1.      Data type of the array
2.      Name of the array and
3.      Size of the array
Consider the following C statement for declaring an array of 100 integers:
int a[100];
There are two basic operations that can be done on an array:  Storage and Retrieval (extraction) of data.  The storage operation is a function which accepts an array a, an index i, and an element x to store the value of x in a[i].  The storage operation is expressed as a[i] = x;  where as the extraction operation accepts an array a, and an index i, and returns an element of the array.  It is expressed in C as a[i].

Every array has a limitation in storing its elements - it can hold only a fixed number of elements in it.  It’s size or range is set by two limits called Upper Bound (UB) and Lower Bound (LB).  The smallest element of an array’s index is called its Lower Bound and in C is always 0, and the highest element is called its Upper Bound.

If L is the lower bound of an array and U is the upper bound, the number of elements in the array, called its range is given by (U-L+1).  For instance, if L is 0, and the U is 99, then the range is 99-0+1 = 100.  An important feature of a C array is that neither the upper bound nor the lower bound may be changed during the time of program execution.

Setting the Upper Bound for an Array:

An array can be declared using the data type of its elements and the Upper Bound.  The lower bound is always fixed at 0, and the upper bound is fixed at the time the program is written.  One useful technique is to declare a bound as a constant identifier, so that the work required to modify the size of an array is minimized.  Consider the following example:
#define NUMELTS 100
int a[NUMELTS];
for(i=0; i<NUMELTS; a[i++]=0);

In this example, the first line defines a constant NUMELTS with value 100.  Then in the second line, the array a is declared using the data type int and the constant NUMELTS.  Then in the third line, a ‘for’ loop statement is used to initialize the elements of the array with 0.  Now, only a single change in the constant definition is required to change the Upper Bound of the array as well as its initialization.

Structure in C:

A structure is a group of items in which each item is identified by its own identifier called member of the structure.  Each item in the structure may be of different types.  Using a structure, we can store a list of data items of a particular thing or person.  Hence, in some programming languages, a structure is called as ‘Record’, and its members as ‘Fields’.

Defining a Structure:

A Structure must be defined as a user-defined data type that contains variables of different types in it.  A group of variables are declared under the keyword struct and are called members of the structure.  Each and every member is defined using a data type and a name.  Following is the syntax for defining a structure:

struct Struc_tag
{
     datatype var-1;
     datatype var-2;
     ...
     ...
     ...

     datatype var-n;
}
where,
struct         -     is a keyword to indicate that the following is a structure definition.
Struc_tag   -     is the name given to the new data type defined by the user.
Body         -     Body of the structure contains a block of declarative statements for  defining a group of variables, which are enclosed within curly braces.
An example for a structure data type is as follows:
            struct nametype
       {
            char first_name[10];
       char midinit;
            char last_name[20];
       };
In this example, the name of the structure is nametype, which contains three members: first_name, midinit and last_name.  The first_name and last_name are arrays of characters, which will store the first name and last name of a person, and the midinit is a character field for storing the initial.

Structure Variables:

Structure variables are variables of type structure.  Variables must be declared using structure data type in order to make use of a structure, i.e.,  a structure data type can’t be directly used for storing values in its members; instead a variable of type structure can be used.

By declaring a structure variable, we allocate required memory locations for storing all the members of the structure in memory.  The amount of memory required by a structure is the sum of storage specified by each of its members.

Structure variables can be declared in two different ways: Implicitly and Explicitly.  In the first method, a variable can be declared in the structure definition itself.  At the end of the structure definition, i.e., after the closing brace of structure definition, we can include the structure variables.  The example for the first method is as follows:
            struct nametype
      {
           char first_name[10];
      char midinit;
           char last_name[20];
      } name;

In the second method, we declare the structure variables in a separate line apart from the structure definition.  Here, we define the structure first, and then using that newly defined structure data type, we declare structure variables (one or more).  Example for this method of defining structure variable is as follows:
            struct nametype
      {
           char first_name[10];
      char midinit;
           char last_name[20];
      };
      struct nametype e_name, s_name;

In the above example, the structure type nametype is defined separately first.  Then the structure variables e_name and s_name are declared next in a separate statement.  The same structure nametype can also be used for declaring more variables later.

Storing and Accessing data from a Structure:

Structure variables can be used for storing values (a set of values of different data types) after their declaration in a program.   Following is the notation used for accessing the members of a structure:
      
StrcutureVariable.MemberVariable;

Use the dot (.) operator for accessing the members of a structure.  The syntax includes the structure variable first, followed by a dot (.) operator and then the member variable.

Examples for using structure is given below.  They make use of the structure variables (e_name and s_name) defined above:
            e_name.first_name = “Rahul”;
      e_name.last_name = “Dravid”;
      s_name.first_name = “Sachin”;
      s_name.last_name = “Tendulkar”;
      printf(“%s %c %s”, e_name.first_name, e_name.midinit, e_name.last_name);

Here is a video presentation on Defining and Using Structures:

One Response so far

  1. Anonymous says:

    I don't even know how I finished up here, but I thought this post
    was once great. I do not realize who you are however certainly you're going to a well-known blogger for those
    who are not already. Cheers!

    Have a look at my blog post :: minecraft

Leave a Reply