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 booksA 100.0 320
B 210.25 452
U have entered
A 100.0 320
B 210.25 452
===========================================================================
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);
}
===========================================================================
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
- Declaration of structure
- Accessing of structure's elements.
The following statement declares the structure type,
struct struct_name {
structure element 1;
structure element 2;
.....................
}
struct book b1 = {"Basic C", 130.00, 234};
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);
====================================================================
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);
}
====================================================================
Structure pointer
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);
}
====================================================================
Uses of Structures
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:
Post a Comment