0% found this document useful (0 votes)
51 views4 pages

6 Structure & Union Final

In C, structures are used to group different variable types together under a single name. For example, a "telephone" structure could contain a string for the name and an integer for the number. Structures allow new data types to be created by declaring the structure name and its elements. Structure variables are then declared to access the elements using a dot operator. Unions are similar to structures but store all elements in the same memory location, so only one element can be accessed at a time. Both structures and unions allow grouping of related data for organized storage and access.

Uploaded by

Vipin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

6 Structure & Union Final

In C, structures are used to group different variable types together under a single name. For example, a "telephone" structure could contain a string for the name and an integer for the number. Structures allow new data types to be created by declaring the structure name and its elements. Structure variables are then declared to access the elements using a dot operator. Unions are similar to structures but store all elements in the same memory location, so only one element can be accessed at a time. Both structures and unions allow grouping of related data for organized storage and access.

Uploaded by

Vipin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter

#
Structure and Union
Structure...
In the C language structures are used to group together different types
of variables under the same name.
For example you could create a structure telephone: which is made up
of a string (that is used to hold the name of the person) and an integer
(that is used to hold the telephone number).
We can declare any variable as a structure data type, which will hold
value of the elements of structure.
Example:
struct telephone
{
char *name;
Note: the; behind the
int number;
last bracket.
};
With the declaration of the structure you have created a new type, called
telephone. Before you can use the type telephone you have to create a
variable of the type telephone.
Example:
struct student
{
char name[50];
int rollno;
int marks[3];
}stu1, stu2;
Here stu1 and stu2 are two structure variables. Another way to declare a structure
is as follow.
struct student
{
char name[50];
int rollno;
int marks[3];

};
struct student stu1, stu2;
Accessing Structure elements:
Elements of the structure are accessed by using dot(.) operator.
/*Program to read student data and display it*/
# include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
char name[50];
int rollno;
int marks[3]
}stu1;
printf(\n Enter name:);
scanf(%s,stu1.name);
printf(\n Enter rollno:);
scanf(%d,&stu1.rollno);
printf(\n Enter marks[0], marks[1], marks[2]:);
scanf(%d%d%d,&stu1.marks[0], &stu1.marks[1], &stu1.marks[2]);
printf(\n\n You have entered following data:);
printf(\n Name: %s,stu1.name);
printf(\n Roll Number: %d, stu1.rollno);
printf(\n Marks1: %d, stu1.marks[0]);
printf(\n Marks2: %d, stu1.marks[1]);
printf(\n Marks3: %d, stu1.marks[2]);
getch();
}

Example:
#include<stdio.h>
struct telephone
{
char *name;
int number;

};
int main()
{
struct telephone index;
return 0;
}
To access the members of the structure telephone, you must use a dot
between the structure name and the variable name (variables: name or
number.)
Example:
#include<stdio.h>
struct telephone
{
char *name;
int number;
};
int main()
{
struct telephone index;
index.name = "Jane Doe";
index.number = 12345;
printf("Name: %s\n", index.name);
printf("Telephone number: %d\n", index.number);
return 0;
}
Unions
A union is like a structure in which all members are stored at the same
address.
Members of a union can only be accessed one at a time. The union data type
was invented to prevent memory fragmentation.
The union data type prevents fragmentation by creating a standard size for
certain data.
Just like with structures, the members of unions can be accessed with the .
and -> operators.
Example:
#include<stdio.h>
typedef union myunion

{
double PI;
int B;
}MYUNION;
int main()
{
MYUNION numbers;
numbers.PI = 3.14;
numbers.B = 50;
return 0;
}

You might also like