ASIC Verification: C Structures

Monday, February 25, 2008

C Structures

We already know that arrays are many variables of the same type grouped together under the same name. 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 book which is made up of a string for the name, float for the prize and an integer for pages. Here is how you would create that book structure in C:


===========================================================================
main()
{
char name[2];
float price[2];
int i,pages[2];

printf("Enter the name, price and pages of 2 books\n");

for(i=0; i<=1; i++)
scanf("%s%f%d", &name[i], &price[i], &pages[i]);
printf("U have entered\n");
for(i=0; i<=1; i++)
printf("%c %f %d\n", name[i], price[i], pages[i]);

}
===========================================================================

and here is the sample run:

Enter the names, prizes and pages of 2 books
A 100.0 320
B 210.25 452

U have entered
A 100.0 320
B 210.25 452

This approach allows you to store name, prize and pages of a book. But the program becomes more difficult to handle as the number of items relating to the book go on increasing. A structure contains a number of data types grouped together, these data types may be or may not be of the same type. The following example illustrates this data type:
===========================================================================
main()
{
struct book
{
char name;
float price;
int pages;
}b1, b2;

printf ("Enter name, price and pages of 2 books\n");
scanf("%s%f%d", &b1.name, &b1.price, &b1.pages);
scanf("%s%f%d", &b2.name, &b2.price, &b2.pages);

printf ("And this is what u entered\n");
printf("%c %f %d\n", b1.name, b1.price, b1.pages);
printf("%c %f %d\n", b2.name, b2.price, b2.pages);
}
===========================================================================
and here is the output;
Enter name, price and pages of 2 books
A 100.0 320
B 210.25 452

And this is what u entered
A 100.0 320
B 210.25 452

The program demonstrates two fundamentals aspects of structure
  1. Declaration of structure
  2. Accessing of structure's elements.
Declaring a structure

The following statement declares the structure type,

struct struct_name {
structure element 1;
structure element 2;
.....................
}

Once the new structure data type has been defined, one or more variables can be declared to be of that type. It makes available space to hold all the elements in the structure. in this case, 7 bytes. one for name, 4 for price and 2 for pages. These bytes are always in adjacent memory locations. Like variables, arrays structure variables can also be initialized in the following way:

struct book b1 = {"Basic C", 130.00, 234};

Accessing structure elements

Having declared the structure type and the structure variables, let us see how the elements of the structure can be accessed. Structure use a dot(.) operator to access their elements like b1.pages, b2.price etc. Whatever be the elements of the structure, they are always stored in contiguous memory locations.

Array of Structure

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, thats why we go for array of structures.
====================================================================
main()
{
struct book
{
char name;
float price;
int pages;
} b[100];
====================================================================
Features of Structure

The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. For example,
====================================================================
struct employee e1 = {"Suresh", 27, "San diego"};
struct employee e2, e3;

strcpy (e2.name, e1.name);
e2.age = e1.age;


/* copying all elements */

e3 = e2;

printf("%s %d %s\n", e1.name, e1.age, e1.place);
printf("%s %d %s\n", e2.name, e2.age, e2.place);
printf("%s %d %s\n", e3.name, e3.age, e3.place);
====================================================================
C doesn't allow assigning the contents of one array to another by just equating the two. As we saw in the previous topic, for copying arrays we have to copy the contents of the array element by element. This copying of all structure element at one go has been possible only because the structure elements are stored in contiguous memory locations.One structure can be nested in another structure.
====================================================================
main()
{
struct address
{
char phone[15];
char city[10];
};

struct emp
{
char name[10];
struct address a;
};
struct emp e = {"Suresh", "12345", "Tenkasi"};

printf("My name is %s\n", e.name);
printf("My phone number is %s\n", e.a.phone);
printf("My native place is %s\n", e.a.city);
}
====================================================================
Like an ordinary variable, we can also pass the structure variable into the function either pass individual structure elements or the entire structure at one go.

Structure pointer

The way we can have a pointer pointing to an integer, similarly we can have a pointer pointing to a structure. Such pointers are known as "Structure Pointers".

Let us have a look at the following program which demonstrates the usage of a SP.
====================================================================
main()
{
struct book
{
char name[40];
char author[25]
};

struct book b1 = {"VLSI Design", "CS"};

struct book *ptr;

ptr = &b1;
printf("%s %s\n", ptr->name, ptr->author);
}
====================================================================
we can't use ptr.name, because ptr is not a structure variable it is pointer to a structure, and the dot operator requires a structure variable on its left. In such cases, C provides an operator ->, called an "arrow operator" to refer to the structure element.

Uses of Structures

Structures are useful in Database management, that is, to maintain data about employees in an organization, books in library, items in a store etc.

What is the difference b/w Structures and Unions?

Unions are like structures except they use less memory. If you create a structure with a double that is 8 bytes and an integer that is 4 bytes then the total size will still only be 8 bytes. This is because they are on top of each other. If you change the value of the double then the value of the integer will change and if you change the value of the integer then the value of the double will change.

Questions

What is a structure?
What are the differences between structures and arrays?
What is difference between Structure and Unions?
What is the similarity between a Structure, Union and enumeration?
Can a Structure contain a Pointer to itself?
How can we check whether the contents of two structure variables are same or not?
How would you use qsort() function to sort an array of structures?
What is the difference between class and structure?


No comments: