Wednesday, November 2, 2011

Array of Structures in C programming language

Our sample program showing usage of structure is rather simple minded. All it does is, it receives values into various structure elements and output these values. But that’s all we intended to do anyway... show how structure types are created, how structure variables are declared and how individual elements of a structure variable are referenced.
In our sample program, to store data of 100 books we would be required to use 100 different structure variables from b1 to b100, which is definitely not very convenient. A better approach would be to use an array of structures. Following program shows how to use an array of structures.

/* Usage of an array of structures */
main( )
{
struct book
{
char name ;
float price ;
int pages ;
} ;
struct book b[100] ;
int i ;
for ( i = 0 ; i <= 99 ; i++ )
{
printf ( "\nEnter name, price and pages " ) ;
scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
}
for ( i = 0 ; i <= 99 ; i++ )
printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages ) ;
}
linkfloat( )
{
float a = 0, *b ;
b = &a ; /* cause emulator to be linked */
a = *b ; /* suppress the warning - variable not used */
}

Now a few comments about the program:
(a) Notice how the array of structures is declared...
struct book b[100] ;
This provides space in memory for 100 structures of the type struct book.
(b) The syntax we use to reference each element of the array b is similar to the syntax used for arrays of ints and chars. For example, we refer to zeroth book’s price as b[0].price. Similarly, we refer first book’s pages as b[1].pages.
(c) It should be appreciated what careful thought Dennis Ritchie has put into C language. He first defined array as a collection of similar elements; then realized that dissimilar data types that are often found in real life cannot be handled using arrays, therefore created a new data type called structure. But even using structures programming convenience could not be achieved, because a lot of variables (b1 to b100 for storing data about hundred books) needed to be handled. Therefore he allowed us to create an array of structures; an array of similar data types which themselves are a collection of dissimilar data types. Hats off to the genius!
(d) In an array of structures all elements of the array are stored in adjacent memory locations. Since each element of this array is a structure, and since all structure elements are always stored in adjacent locations you can very well visualise the arrangement of array of structures in memory. In our example, b[0]’s name, price and pages in memory would be immediately followed by b[1]’s name, price and pages, and so on.
(e) What is the function linkfloat( ) doing here? If you don’t define it you are bound to get the error "Floating Point Formats Not Linked" with majority of C Compilers. What causes this error to occur? When parsing our source file, if the compiler encounters a reference to the address of a float, it sets a flag to have the linker link in the floating-point emulator. A floating point emulator is used to manipulate floating point numbers in runtime library functions like
scanf( ) and atof( ). There are some cases in which the reference to the float is a bit obscure and the compiler does not detect the need for the emulator. The most common is using scanf( ) to read a float in an array of structures as shown in our program.
How can we force the formats to be linked? That’s where the linkfloat( ) function comes in. It forces linking of the floating-point emulator into an application. There is no need to call this function, just define it anywhere in your program.

2 comments:

  1. hello sir

    after compling this program the error is coming so can you help me to solve this problem

    the error is
    "" [Error] ISO C++ forbids declaration of 'linkfloat' with no type [-fpermissive] ""


    thank you.

    ReplyDelete
  2. This exact code works just fine for me. (I copy-pasted it.)
    Its probably due to your compiler settings. It says that you have add type before your first mention of linkfloat(), ie "float" in this case.

    ReplyDelete