0% found this document useful (0 votes)
29 views

L10- Structures in C.ppt

This document provides an overview of structures in C programming, explaining their definition, usage, and how to declare and access structure variables. It also covers related topics such as arrays of structures, nested structures, pointers to structures, unions, and enumerations. The content is intended for first-year students at IMT and IMG, and emphasizes the importance of reference books for detailed understanding.

Uploaded by

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

L10- Structures in C.ppt

This document provides an overview of structures in C programming, explaining their definition, usage, and how to declare and access structure variables. It also covers related topics such as arrays of structures, nested structures, pointers to structures, unions, and enumerations. The content is intended for first-year students at IMT and IMG, and emphasizes the importance of reference books for detailed understanding.

Uploaded by

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

C Programming

Module-10: Structure in C

IMT and IMG 1st Year

Instructor : Dr. Santosh Singh Rathore


ABV-IIITM Gwalior
Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content for the slides have been taken from the various sources and
from the reference books.

Students need to follow reference books to get the thorough details


of the concepts discussed in slides
Structure
• Structure is another user defined data type available in C that allows to combine
data items of different kinds.
• Structures are used to represent a record. Suppose you want to keep track of
your books in a library.
• You might want to track the following attributes about each book −

• Title
• Author
• Subject
• Book ID

• Here structure is useful.


Defining a Structure
• Definition of a structure creates a template or format that describes the
characteristics of its members.
• All the variables that would be declared of this structure type, will take the form
of this template.
• General syntax of a structure definition is

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.

• enum week{sunday, monday, tuesday, wednesday, thursday, friday,


saturday};
• enum week day;
Enum in C
Enum in C
#include<stdio.h>
enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};
enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};
int main() {
printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue,
Wed, Thur, Fri, Sat, Sun);
printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond ,
Tues, Wedn, Thurs, Frid, Satu, Sund);
return 0;
}

Output
The value of enum week: 10111213101617
The default value of enum day: 0123181112
Thank you for your attention!

< Questions?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy