Modue4 Lect1,2,3,4
Modue4 Lect1,2,3,4
struct tagname{
member1 definition;
member2 definition;
...
membern definition;
} [one or more structure variables];
struct A
{
int a;
int* b;
char c;
char* d;
};
int main()
{
struct A a;
printf("Size of struct A: %lu\n", sizeof(struct A));
printf("Size of object a: %lu\n", sizeof(a));
return 0;
}
Output
Size of struct A: 32
Size of object a: 32 //instead of 21 bytes compiler gives 32 bytes as answer
Structure Padding
• To find the actual size, you need to understand two concepts of padding
• Processor doesn't read 1byte at a time from memory. It reads 1 word at a
time. In 32 bit processor, it can access 4 bytes at a time which means word
size is 4 bytes.
• Similarly in a 64 bit processor, it can access 8 bytes at a time which means
word size is 8 bytes.
• So in previous example, It aligns till the boundary of maximum memory
allocated.
• Here we find that max memory allocated is 8 Bytes, thus all the data
members acquire 8 Bytes and the total size is 32 Bytes.
Example for structure Padding
struct B { Output:
int* b; Size of struct B: 24
char c; Size of object b: 24
int a;
char* d;
};
int main()
{
struct B b;
printf("Size of struct B: %lu\n",
sizeof(struct B));
printf("Size of object b: %lu\n",
sizeof(b));
return 0;
}
#include<stdio.h> #include<stdio.h>
struct book
struct book {
{ double issno ;
double price ;
double issno ; int volumeno;
double price ; };
main( )
double volumeno; {
}; struct book b1;
main( )
printf("\n the sizeof the structure is
{ %d",sizeof(b1));
struct book b1; }
Output (here padding takes place)
printf("\n the sizeof the structure is
%d",sizeof(b1));
}
Accessing the members of a Structure
• To access the structure, you must create a variable of
it.
• Use the struct keyword inside the main() method,
followed by the name of the structure and then the
name of the structure variable
Structure variable declaration
struct person struct person
{
char name[50];
{
int age; char name[50];
float salary; int age;
};
float salary;
int main() } person1, person2;
{
struct person person1,
person2;
return 0;
}
Accessing members of a structure
• There are two types of operators used for accessing
members of a structure.
– Member operator(.)
– Structure pointer operator(->)
structure_variable_name.member_name
person2.age
Example Program
strcpy( Book2.title, "Telecom Billing");
#include <stdio.h> strcpy( Book2.author, "Zara Ali");
#include <string.h>
strcpy( Book2.subject, "Telecom Billing
struct Books { Tutorial");
char title[50]; Book2.book_id = 6495700;
char author[50]; printf( "Book 1 title : %s\n", Book1.title);
char subject[100]; printf( "Book 1 author : %s\n",
long int book_id; Book1.author);
}; printf( "Book 1 subject : %s\n",
Book1.subject);
int main( )
{ printf( "Book 1 book_id : %d\n",
struct Books Book1; Book1.book_id);
struct Books Book2; printf( "Book 2 title : %s\n", Book2.title);
strcpy( Book1.title, "C Programming"); printf( "Book 2 author : %s\n",
strcpy( Book1.author, "Nuha Ali"); Book2.author);
strcpy( Book1.subject,"C printf( "Book 2 subject : %s\n",
Programming”); Book2.subject);
Book1.book_id = 6495407; printf( "Book 2 book_id : %d\n",
Book2.book_id);
return 0;}
Initialize structure members
• Structure members cannot be initialized with declaration. For
example the following C program fails in compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
}
Initialization of structure variables
struct Patient
{
float height;
int weight;
int age;
};
struct book
{
char name[10] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
#include <stdio.h>
#include <string.h>
struct Employee
{
char name[20];
char gender;
int empid;
};
void main()
{
struct Employee emp={"Anny",'f',15698};
printf("\n the employee name is %s and gender is %c and emp id
is %d",emp.name,emp.gender,emp.empid);
}
Designated Initialization allows structure members to
be initialized in any order.
#include <stdio.h>
#include <string.h>
struct Employee
{
char name[20];
char gender;
int empid;
};
void main()
{
struct Employee emp={.gender='f',.name="Anny",.empid=15698};
printf("\n the employee name is %s and gender is %c and emp id is
%d",emp.name,emp.gender,emp.empid);
}
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
} record;
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
output
name = jeru phone = 531046
city = nagpur pin = 10
#include <stdio.h> scanf("%f",&b1.price);
struct author printf("\n enter the author details, name:");
scanf("%s",b1.details.aname);
{ printf("\n enter the no of pages in the book");
char aname[20]; scanf("%d",&b1.details.pages);
int pages; printf("\n details....");
}; printf("\n Book name:%s",b1.name);
printf("\n Book price:%f",b1.price);
printf("\n Author name:%s",b1.details.aname);
struct book printf("\n No of pages:%d",b1.details.pages);
{ }
char name[10] ; Output
float price ;
struct author details;
};
void main()
{
struct book b1,b2;
printf("\n enter the name of the book
");
scanf("%s",b1.name);
printf("enter the price details:");
#include <stdio.h>
struct author for(i=0;i<n;i++)
{
char aname[20];
{
int pages; printf("\n details of
};
book%d",i+1);
struct book printf("\n Book
{
char name[10] ; name:%s",b1[i].name);
float price ; printf("\n Book
struct author details;
}; price:%f",b1[i].price);
void main()
{
printf("\n Author
struct book b1[10]; name:%s",b1[i].details.aname);
int n,i;
printf("\n enter how many book details are you
printf("\n No of
going to collect"); pages:%d",b1[i].details.pages);
scanf("%d",&n);
for(i=0;i<n;i++) }
{ }
printf("\n enter the name of the book%d",i+1);
scanf("%s",b1[i].name);
printf("enter the price details:");
scanf("%f",&b1[i].price);
printf("\n enter the author details, name:");
scanf("%s",b1[i].details.aname);
printf("\n enter the no of pages in the book");
scanf("%d",&b1[i].details.pages);
}
To search for a book in the list
#include <stdio.h> for(i=0;i<n;i++)
#include <string.h>
struct author
{
{ printf("\n details of book%d",i+1);
char aname[20]; printf("\n Book name:%s",b1[i].name);
int pages; printf("\n Book price:%f",b1[i].price);
};
printf("\n Author name:%s",b1[i].details.aname);
struct book printf("\n No of pages:%d",b1[i].details.pages);
{ }
char name[10] ; printf("\n enter the name of the books which u
float price ; want to search");
struct author details;
};
fflush(stdin);
void main() scanf("%s",Sbook);
{ for(i=0;i<n;i++)
struct book b1[10]; {
int n,i;
int flag=0;
if(strcmp(b1[i].name,Sbook)==0)
char Sbook[20]; {
printf("\n enter how many book details are you going to flag=1;
collect"); break;
scanf("%d",&n);
for(i=0;i<n;i++)
}
{ }
printf("\n enter the name of the book%d",i+1); if (flag==1)
scanf("%s",b1[i].name); printf("\n the book is found");
printf("enter the price details:");
scanf("%f",&b1[i].price);
else
printf("\n enter the author details, name:"); printf("\n book is not found");
scanf("%s",b1[i].details.aname); }
printf("\n enter the no of pages in the book");
scanf("%d",&b1[i].details.pages);
}
Output
enter how many book details are you going to collect2
details of book1
Book name:C
Book price:900.000000
Author name:yashwant
No of pages:1000
details of book2
Book name:Java
Book price:450.000000
Author name:Herbert
No of pages:100
enter the name of the books which u want to searchC
return 0;
}
the typedef keyword to allow users to provide alternative names for
the primitive (e.g., int) and user-defined (e.g struct) data types.
#include<stdio.h> #include<stdio.h>
• Similar to structures
int main()
{
union car car1, car2, *car3;
return 0;
}
Accessing members of a union
• To access price for union variable car1
car1.price
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Difference between union and structure
There is a difference in memory allocation between union
and structure.
STRUCTURE UNION
union Data {
int i;
float f;
char str[20];
};
return 0;
}
Accessing Union Members (Example 2)
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20]; OUTPUT
};
data.i : 10
int main( ) data.f : 220.500000
{ data.str : C Programming
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);