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

Structure & Array of Structures

Uploaded by

21l121
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Structure & Array of Structures

Uploaded by

21l121
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

STRUCTURE & UNION

• Structure
• Nested structures
• Pointer and Structures BASICS
• Array of structures
• Self referential structures
• Dynamic memory FILE ARRAYS &
allocation PROCESSING STRINGS
• Singly linked list
• typedef
• Union
• Storage classes and STRUCTURE FUNCTIONS
Visibility (scope).
& UNION & POINTERS
Structure
Unit IV

Book Referred : T1

6/25/2022 C.P.Shabariram 2
Outline
• Structure
• Syntax
• Declaration & Access
• typedef
• Array of Structures

6/25/2022 C.P.Shabariram 3
Structure
• User-defined data type
• store the collection of different data
types
• Each element of a structure is called a
member
• struct keyword is used to define the
structure

6/25/2022 C.P.Shabariram 4
Syntax
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};

6/25/2022 C.P.Shabariram 5
Example

6/25/2022 C.P.Shabariram 6
Declaring structure variable
• There are two ways to declare structure
variable:
• By struct keyword within main() function
– No. of variables are not fixed
– It provides the flexibility to declare the structure
variable many times.
• By declaring a variable at the time of defining
the structure
– No. of variables are fixed
– It saves your code to declare a variable in main()

6/25/2022 C.P.Shabariram 7
Accessing members of the structure
• There are two ways to access structure
members:
• . (member or dot operator)
• -> (structure pointer operator)

6/25/2022 C.P.Shabariram 8
Example : structure
#include<stdio.h>
#include <string.h>
struct subject
{ int code; strcpy(s2.name, “C Lab”);
char name[50]; printf( "code : %d ", s1.code);
}s1; printf( "name : %s", s1.name);
printf( “\ncode : %d ", s2.code);
int main( )
printf( "name : %s", s2.name);
{ return 0;
struct subject s2; }
s1.code=3251;
strcpy(s1.name, “C”);
s2.code=3271;
01/22/2019 C.P.Shabariram 9
Structure Padding
• Adds one or more empty bytes between the memory
addresses to align the data in memory
• The processor does not read 1 byte at a time.
– It reads 1 word at a time.
• a 32-bit processor, the processor reads 4 bytes at a
time, which means that 1 word is equal to 4 bytes.
• a 64-bit processor, the processor reads 8 bytes at a
time, which means that 1 word is equal to 8 bytes.

01/22/2019 C.P.Shabariram 10
Structure Padding
struct student
{
char a; // 1 byte
char b; // 1 byte
int c; // 4 bytes
}s1;

• sizeof(s1) => 8

01/22/2019 C.P.Shabariram 11
Structure Padding
struct student
{
char a; // 1 byte
int c; // 4 bytes
char b; // 1 byte

}s1;

• sizeof(s1) => 12
01/22/2019 C.P.Shabariram 12
typedef
• provide some meaningful names to the
already existing variable in the C
• redefine the name of an already existing
variable.
• Syntax:
typedef <existing_name> <another_name>;
• Example
typedef int i;
6/25/2022 C.P.Shabariram 13
typedef with structures
struct subject typedef struct subject
{ {
char name[20]; char name[20];
int code; int code;
}; } sub;
typedef struct subject sub; sub s1,s2;
sub s1, s2;

6/25/2022 C.P.Shabariram 14
typedef with Pointers
• another name to the pointer variables
• Example
int* ptr;
typedef int* ptr;
• ptr p1, p2 ;
• p1 and p2 are the variables of type 'ptr'.

6/25/2022 C.P.Shabariram 15
Array of Structures
• The collection of multiple structures variables
where each variable contains information
about different entities.
• Store information about multiple entities of
different data types.
• Another name: Collection of structures.

6/25/2022 C.P.Shabariram 16
Example

6/25/2022 C.P.Shabariram 17

You might also like