L10- Structures in C.ppt
L10- Structures in C.ppt
Module-10: Structure in C
Content for the slides have been taken from the various sources and
from the reference books.
• Title
• Author
• Subject
• Book ID
struct tagname {
data type memberl;
datatype member2;
datatype memberN;
};
Defining a Structure
• Here struct is a keyword, which tells the compiler that a structure is being
defined.
• memberl, member2 ...............memberN are known as members of the structure.
• These members can be of any data type like int, char, float, array, pointers or
another structure type.
• tagname is the name of the structure
• It is used further in the program to declare variables of this structure type.
• Definition of a structure provides one more data type in addition to the built in
data types. We can declare variables of this new data type that will have the
format of the defined structure.
• Definition of a structure template does not reserve any space in memory for the
members, reserved only when actual variables of this structure type are declared.
Defining a Structure
• The member names inside a structure should be different from one another but
these names similar to any other variable name declared outside the structure.
• The member names of two different structures may also be same.
struct student{
char name [20] ;
int rollno;
float marks;
};
• Declaring Structure Variables
• We can declare structure variables in two ways-
• With structure definition
• Using the structure tag
With Structure Definition
Initialization of Structure Variables
Accessing Members of a Structure
• We use the dot ( . ) operator which is also as the period or membership operator.
• The format for accessing a structure member is structvariable.member
Accessing Members of a Structure
Assignment of Structure Variables
Storage of Structures in Memory
Array of Structures
• Array of structures can be declared as
struct student stu[ 10];
•stu is an array of 10 elements, each of which is a structure of type struct student,
means each element of stu has 3 members, which are name, rollno and marks.
•These structures can be accessed through subscript notation.
Array of Structures
Arrays Within Structures
• We can have an array as a member of structure.
• In structure student, we have taken the member name array of characters.
• Now we'll declare another array inside the structure student.
Arrays Within Structures
Nested Structures (Structure Within Structure)
• The members of a structure can be of any data type including another structure type i.e.
we can include a structure within another structure.
• A structure variable can be a member of another structure. This is called nesting of
structures.
Nested Structures (Structure Within Structure)
Pointers to Structures
• We can have pointer to structure, which can point to the start address of a
structure variable.
• These pointers are called structure pointers and can be declared
Pointers to Structures
Pointers to Structures
Pointers Within Structures
Structures And Functions
Union
• A union is a user-defined type similar to structs in C except for one key
difference.
• Structs allocate enough space to store all its members, wheres unions
allocate the space to store only the largest member.
• We use the union keyword to define unions. Here's an example:
union car
{
char name[50];
int price;
};
• The above code defines a derived type union car.
Create union variables
Enum in C
• Enumeration is a user defined datatype in C language.
• It is used to assign names to the integral constants which makes a
program easy to read and maintain.
• The keyword “enum” is used to declare an enumeration.
• Here is the syntax of enum in C language,
enum enum_name{const1, const2, ....... };
• The enum keyword is also used to define the variables of enum type.
There are two ways to define the variables of enum type as follows.
Output
The value of enum week: 10111213101617
The default value of enum day: 0123181112
Thank you for your attention!
< Questions?