Tutorials
C LanguageIn this tutorial you will learn about C Programming - Structures and Unions, Giving values to members, Initializing structure, Functions and structures, Passing structure to elements to functions, Passing entire function to functions, Arrays of structure, Structure within a structure and Union.
As mentioned earlier the members themselves are not variables they should be linked to structure variables in order to make them meaningful members. The link between a member and a variable is established using the member operator ‘.’ Which is known as dot operator or period operator.
For example:
Book1.price
Is the variable representing the price of book1 and can be treated like any other ordinary variable. We can use scanf statement to assign values like
scanf(“%s”,book1.file);
scanf(“%d”,& book1.pages);
Or we can assign variables to the members of book1
strcpy(book1.title,”basic”);
strcpy(book1.author,”Balagurusamy”);
book1.pages=250;
book1.price=28.50;
/* Example program for using a structure*/
#include< stdio.h >
void main()
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}newstudent;
printf(“Enter the student information”);
printf(“Now Enter the student id_no”);
scanf(“%d”,&newstudent.id_no);
printf(“Enter the name of the student”);
scanf(“%s”,&new student.name);
printf(“Enter the address of the student”);
scanf(“%s”,&new student.address);
printf(“Enter the cmbination of the student”);
scanf(“%d”,&new student.combination);
printf(“Enter the age of the student”);
scanf(“%d”,&new student.age);
printf(“Student information\n”);
printf(“student id_number=%d\n”,newstudent.id_no);
printf(“student name=%s\n”,newstudent.name);
printf(“student Address=%s\n”,newstudent.address);
printf(“students combination=%s\n”,newstudent.combination);
printf(“Age of student=%d\n”,newstudent.age);
}
Like other data type we can initialize structure when we declare them. As for initalization goes structure obeys the same set of rules as arrays we initalize the fields of a structure by the following structure declaration with a list containing values for weach fileds as with arrays these values must be evaluate at compile time.
Example:
Struct student newstudent
{
12345,
“kapildev”
“Pes college”;
“Cse”;
19;
};
this initializes the id_no field to 12345, the name field to “kapildev”, the address field to “pes college” the field combination to “cse” and the age field to 19.
We can pass structures as arguments to functions. Unlike array names however, which always point to the start of the array, structure names are not pointers. As a result, when we change structure parameter inside a function, we don’t effect its corresponding argument.
Passing structure to elements to functions:
A structure may be passed into a function as individual member or a separate variable.
A program example to display the contents of a structure passing the individual elements to a function is shown below.
# include < stdio.h >
void main()
{
int emp_id;
char name[25];
char department[10];
float salary;
};
static struct emp1={125,”sampath”,”operator”,7500.00};
/* pass only emp_id and name to display function*/
display(emp1.emp_id,emp1.name);
}
/* function to display structure variables*/
display(e_no,e_name)
int e_no,e_name;
{
printf(“%d%s”,e_no,e_name);
in the declaration of structure type, emp_id and name have been declared as integer and character array. When we call the function display() using display(emp1.emp_id,emp1.name);
we are sending the emp_id and name to function display(0);
it can be immediately realized that to pass individual elements would become more tedious as the number of structure elements go on increasing a better way would be to pass the entire structure variable at a time.
In case of structures having to having numerous structure elements passing these individual elements would be a tedious task. In such cases we may pass whole structure to a function as shown below:
# include stdio.h>
{
int emp_id;
char name[25];
char department[10];
float salary;
};
void main()
{
static struct employee emp1=
{
12,
“sadanand”,
“computer”,
7500.00
};
/*sending entire employee structure*/
display(emp1);
}
/*function to pass entire structure variable*/
display(empf)
struct employee empf
{
printf(“%d%s,%s,%f”, empf.empid,empf.name,empf.department,empf.salary);
}
It is possible to define a array of structures for example if we are maintaining information of all the students in the college and if 100 students are studying in the college. We need to use an array than single variables. We can define an array of structures as shown in the following example:
structure information
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
student[100];
An array of structures can be assigned initial values just as any other array can. Remember that each element is a structure that must be assigned corresponding initial values as illustrated below.
#include< stdio.h >
{
struct info
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
struct info std[100];
int I,n;
printf(“Enter the number of students”);
scanf(“%d”,&n);
printf(“ Enter Id_no,name address combination age\m”);
for(I=0;I < n;I++)
scanf(%d%s%s%s%d”,&std[I].id_no,std[I].name,std[I].address,std[I].combination,&std[I].age);
printf(“\n Student information”);
for (I=0;I< n;I++)
printf(“%d%s%s%s%d\n”, ”,std[I].id_no,std[I].name,std[I].address,std[I].combination,std[I].age);
}
A structure may be defined as a member of another structure. In such structures the declaration of the embedded structure must appear before the declarations of other structures.
struct date
{
int day;
int month;
int year;
};
struct student
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
structure date def;
structure date doa;
}oldstudent, newstudent;
the sturucture student constains another structure date as its one of its members.
Unions like structure contain members whose individual data types may differ from one another. However the members that compose a union all share the same storage area within the computers memory where as each member within a structure is assigned its own unique storage area. Thus unions are used to observe memory. They are useful for application involving multiple members. Where values need not be assigned to all the members at any one time. Like structures union can be declared using the keyword union as follows:
union item
{
int m;
float p;
char c;
}
code;
this declares a variable code of type union item. The union contains three members each with a different data type. However we can use only one of them at a time. This is because if only one location is allocated for union variable irrespective of size. The compiler allocates a piece of storage that is large enough to access a union member we can use the same syntax that we use to access structure members. That is
code.m
code.p
code.c
are all valid member variables. During accessing we should make sure that we are accessing the member whose value is currently stored.
For example a statement such as
code.m=456;
code.p=456.78;
printf(“%d”,code.m);
Would prodece erroneous result.
|
your tutorial is so nice and simple. it is very useful for my to prepare for my interview. thank u one and all. |
|
what is the pointer to constant, constant to pointer & pointer arithmetic operator explain with example in detail |
| What is difference between structure and union? |
|
i wanna a easy n brief discussion about function used in structures. can u help me plz............? m waiting of ur precious reply............ |
| i wanna brief discussion about function used in structures...........plz make me clear ovr it |
| how to find adress of structure variable |
| Your Way Of Deccribing Metter Is Very Easy And Sample.Thanks To all. |
| write a non-recursive function in C programming language to reverse a doubly linked list? |
| Write a program in C programming laguage to list the nodes of a binary tree in the following way, List the root, the nodes at depth 1, followed by nodes at depth 2 and so on. |
| i need more discussion and make it easy... |
| Write a program in C program langauge to perform employee pay bill using structure |
| Write a program in C language to perform file copy |
|
in place of (.) can we use (->)pointing to symbol? which one is more suitable? |
| Thanks for such a simple explanation.. |
| I want the applications of Unions. I searched in many sites, but with no good result.. |
|
respected sir/mam i want the detail theory on the topic difference between union and structure in c language |
| I want to detail about regrassion. |
| real world application of union? |
| Your contents are good enough to assist me while eaching. thanx a lot |
|
respected sir/mam i want the detail theory on the difference between union and structure in c language |
| I have already studied C structure but today I learnt new concept in this website so I am happy. Thanks to web designer and hoister |
| Thanks for giving me cleared knowledge about C language. |
| These TUTORIALS brings about a lot of clarity among intuitive students and enables them to clear their conceptions. Its a truly remarkable way of spreading knowledge worldwide. |
| I am not able to understand union. What is the meaning of word 'supercede' in above explanation given by you. Thank you |
| In union, what happens when the information is stored in multifield at single time? |
| Unions save memory then why do we use structures rather than that? |
| Without stucture how can we represent some student's containts in memory location. |
|
suppose there is two structers have almost all same data members , how will u write in single structure ? suppose:- struct x {int a, char b, float d} z; struct y {int a, char b} z; How will you write in a signle structure ? mail me "avirus_944@yahoo.co.in Thanks, -www.eodissa.com |
|
@hritik : that's simply because unions allow access to only one member at a time which is needless to say very limited. @Amaresh : declare the structure y{int a; char b} then declare the structure x as follows x{y FirstStruct; float d}; this way you'll be able to get to {int a; char b} through FirstStruct. |
| Is structure a user-defined data type or derived data type? Why? Plz explain... |