0% found this document useful (0 votes)
14 views40 pages

Reference Material II

Uploaded by

Shri ABCD
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)
14 views40 pages

Reference Material II

Uploaded by

Shri ABCD
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/ 40

Embedded Structure Nesting

In this method, the structure being nested is also


declared inside the parent structure.
Example
struct parent
{ int member1;
struct member_str member2
{ int member_str1;
char member_str2;
... }
... }
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin
, emp.add.phone);
printf("Printing the employee information....\n");
printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s",emp.
name,emp.add.city,emp.add.pin,emp.add.phone);
}
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
//printing first employee information
printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy)
: %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy)
;
return 0;
}
struct child {
int x;
char c;
};
// parent structure declaration
struct parent {
int a;
struct child b;
};

// driver code
int main()
{
struct parent var1 = { 25, 195, 'A' };

// accessing and printing nested members


printf("var1.a = %d\n", var1.a);
printf("var1.b.x = %d\n", var1.b.x);
printf("var1.b.c = %c", var1.b.c);
return 0;
}
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}emp1;
Accessing Nested Structure

• We can access the member of the nested


structure by
Outer_Structure.Nested_Structure.member as
given below:

• e1.doj.dd
• e1.doj.mm
• e1.doj.yyyy
#include <stdio.h>
#include <string.h>
struct Organisation
{
char organisation_name[20];
char org_number[20];
struct Employee
{
int employee_id;
char name[20];
int salary;
} emp;
};
// Driver code
int main()
{
struct Organisation org;
// Print the size of organisation
// structure
printf("The size of structure organisation : %ld\n",
sizeof(org));
org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name, "GeeksforGeeks");
strcpy(org.org_number, "GFG123768");
printf("Organisation Name : %s\n",
org.organisation_name);
printf("Organisation Number : %s\n",
org.org_number);
printf("Employee id : %d\n",
org.emp.employee_id);
printf("Employee name : %s\n",
org.emp.name);
printf("Employee Salary : %d\n",
org.emp.salary);
}
• Note:
• Whenever an embedded nested structure is
created, the variable declaration is
compulsory at the end of the inner structure,
which acts as a member of the outer
structure. It is compulsory that the structure
variable is created at the end of the inner
structure.
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
// variable of structure with pointer defined
struct Student s, *ptr;
int main()
{
ptr = &s;
// Taking inputs
printf("Enter the Roll Number of Student\n");
scanf("%d", &ptr->roll_no);
printf("Enter Name of Student\n");
scanf("%s", &ptr->name);
printf("Enter Branch of Student\n");
scanf("%s", &ptr->branch);
printf("Enter batch of Student\n");
scanf("%d", &ptr->batch);

// Displaying details of the student


printf("\nStudent details are: \n");

printf("Roll No: %d\n", ptr->roll_no);


printf("Name: %s\n", ptr->name);
printf("Branch: %s\n", ptr->branch);
printf("Batch: %d\n", ptr->batch);

return 0;
}
How to Return Structure from a
#include<stdio.h>
Function?
struct wage{
char name[50];
int rs;
};
struct wage employee();
int main(){
struct wage e;
e = employee();
printf("\nWage details of the employee\n");
printf("Name : %s",e.name);
printf("\nWage : %d",e.rs);
return 0;
struct wage employee(){
struct wage e1;

printf("Enter the name of the employee : ");


scanf("%s",e1.name);
printf("\nEnter the wage : ");
scanf("%d",&e1.rs);

return e1;
}
How to Pass Structure by Reference
#include<stdio.h>
struct car
{
char name[20];
int seat;
char fuel[10];
};
void print_struct(struct car *);
int main()
{
struct car tata;
printf("Enter the model name : ");
scanf("%s",tata.name);
printf("\nEnter the seating capacity : ");
scanf("%d",&tata.seat);
printf("\nEnter the fuel type : ");
scanf("%s",tata.fuel);
print_struct(&tata);
void print_struct(struct car *ptr)
{
printf("\n---Details---\n");
printf("Name: %s\n", ptr->name);
printf("Seat: %d\n", ptr->seat);
printf("Fuel type: %s\n", ptr->fuel);
printf("\n");
}
An Array of Structures as Function Arguments
#include<stdio.h>
struct details {
char name[20];
char sec[20];
float per;
};
void print_struct(struct details str_arr[]);
int main()
{
struct details student[3] = {
{
"Aisiri","A",89.6
},
{
"Gourav","B",60.4
},
{
"Samuel","C",98.4
},
};
print_struct(student);
return 0;
void print_struct(struct details str_arr[]) {
int i;

for (i = 0; i < 3; i++) {


printf("Name: %s\n", str_arr[i].name);
printf("Section: %s\n", str_arr[i].sec);
printf("Percentage: %.2f\n", str_arr[i].per);
printf("\n");
}
}
Limitations of C Structures
• In C language, structures provide a method for packing
together data of different types. A Structure is a helpful tool
to handle a group of logically related data items. However, C
structures also have some limitations.
• Higher Memory Consumption: It is due to structure padding.
• No Data Hiding: C Structures do not permit data hiding.
Structure members can be accessed by any function,
anywhere in the scope of the structure.
• Functions inside Structure: C structures do not permit
functions inside the structure so we cannot provide the
associated functions.
• Static Members: C Structure cannot have static members
inside its body.
• Construction creation in Structure: Structures in C cannot
have a constructor inside Structures.
#include<stdio.h> void display(struct employee emp)
{
struct address
printf("Printing the details....\n");
{ printf("%s %s %d
char city[20]; %s",emp.name,emp.add.city,emp.add.pin,emp.add.pho
int pin; ne);
char phone[14]; }
};
struct employee
{
char name[20];
struct address add;
};
void display(struct employee);
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
display(emp);
}

You might also like