[go: up one dir, main page]

0% found this document useful (0 votes)
19 views18 pages

C lecture 6.1

Structures in C are user-defined data types that allow the storage of different data types in a single unit. They can simulate classes and templates, and members can be accessed using the dot operator or the structure pointer operator. Additionally, structures can be nested, passed to functions, and combined into arrays for managing multiple entities.

Uploaded by

devlina.karmakar
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)
19 views18 pages

C lecture 6.1

Structures in C are user-defined data types that allow the storage of different data types in a single unit. They can simulate classes and templates, and members can be accessed using the dot operator or the structure pointer operator. Additionally, structures can be nested, passed to functions, and combined into arrays for managing multiple entities.

Uploaded by

devlina.karmakar
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/ 18

Structures:

Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a
structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information.
The ,struct keyword is used to define the structure.

Defining structure:
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Example:
struct employee
{
int id;
char name[20];
float salary;
};

Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the members or fields of the
structure.
Declaring structure variable:
We can declare a variable for the structure so that we can access the member of the structure easily.
There are two ways to declare structure variable:
By struct keyword within main() function
By declaring a variable at the time of defining the structure.
1st way: It should be declared within the main function.
struct employee
{
int id;
char name[50];
float salary;
};

Now write given code inside the main() function.


struct employee e1, e2;
The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and e2 can be treated in the same
way as the objects in C++ and Java.
2nd way:
Another way to declare variable at the time of defining the structure.
struct employee
{
int id;
char name[50];
float salary;
} e1,e2;

Which approach is good


• If number of variables are not fixed, use the 1st approach. It provides you the flexibility to declare the structure
variable many times.
• If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in main() function.
Accessing members of the structure:
There are two ways to access structure members:
1. By . (member or dot operator)
2. By -> (structure pointer operator)
C Structure example:
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Array of Structure:
An array of structures in C can be defined as the collection of multiple structures variables where each variable contains
information about different entities. The array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of structures.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Type-def declaration:
The C programming language provides a keyword called typedef, which you can use to give a type a new name.
Example:
typedef unsigned char BYTE;
After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned char, for example.
BYTE b1, b2;
typedef unsigned char byte;
We can use typedef to give a name to your user defined data types as well.

Typedef vs #define:
typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as
well, q., you can define 1 as ONE etc.
typedef interpretation is performed by the compiler whereas #define statements are processed by the pre-processor.
#include <stdio.h>
#include <string.h> // Example of type-def declaration
typedef struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;
int main( ) {
Book book;
strcpy( book.title, "C Programming");
strcpy( book.author, "E Balaguruswamy");
strcpy( book.subject, "C Programming for Beginners");
book.book_id = 101;
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
return 0;
}
Nested Structure:
A structure can be nested inside another structure. In other words, the members of a structure can be of any other type including
structure. Here is the syntax to create nested structures.

structure tagname_1
{
member1;
member2;
member3;
...
membern;
structure tagname_2
{
member_1;
member_2;
member_3;
...
member_n;
}, var1
} var2;
#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( )
{
e1.id=101;
strcpy(e1.name, "Sidharth Malhotra");
e1.doj.dd=16;
e1.doj.mm=01;
e1.doj.yyyy=1985;
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;
}
Structures and functions:
We can pass structures as arguments to a function. In fact, we can pass, individual members, structure variables, a
pointer to structures etc. to the function. Similarly, functions can return either an individual member or structures
variable or pointer to the structure.

Passing Structure Members as arguments to Function


Passing Structure Variable as Argument to a Function
Passing Structure Pointers as Argument to a Function
Array of Structures as Function Arguments

These are 4 different by which structures can be passed through a function.


#include<stdio.h> // Passing Structure Members as arguments to Function
struct student
{
char name[20];
int roll_no;
int marks;
};
void print_struct(char name[], int roll_no, int marks);
int main()
{
struct student stu = {"Tintin", 1, 78};
print_struct(stu.name, stu.roll_no, stu.marks);
return 0;
}
void print_struct(char name[], int roll_no, int marks)
{
printf("Name: %s\n", name);
printf("Roll no: %d\n", roll_no);
printf("Marks: %d\n", marks);
printf("\n");
}
#include<stdio.h> //Passing Structure Variable as Argument to a Function
struct student
{
char name[20];
int roll_no;
int marks;
};
void print_struct(struct student stu);
int main()
{
struct student stu = {"Sinchan", 10, 69};
print_struct(stu);
return 0;
}
void print_struct(struct student stu)
{
printf("Name: %s\n", stu.name);
printf("Roll no: %d\n", stu.roll_no);
printf("Marks: %d\n", stu.marks);
printf("\n");
}
#include<stdio.h> //Passing Structure Pointers as Argument to a Function
struct employee
{
char name[20];
int age;
char doj[10];
char designation[20];
};
void print_struct(struct employee *);
int main()
{
struct employee dev = {"Jane", 25, "25/2/2015", "Developer"};
print_struct(&dev);
return 0;
}
void print_struct(struct employee *ptr)
{
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Date of joining: %s\n", ptr->doj);
printf("Designation: %s\n", ptr->designation);
printf("\n");
}
#include<stdio.h> //Array of Structures as Function Arguments
struct company
{
char name[20];
char ceo[20];
float revenue;
float pps;
};
void print_struct( struct company str_arr[]);
int main()
{
struct company companies[3] = {
{"Cadbury", “Drik Van de Put", 999999999, 1300 },
{“Nestle", “Ulf Mark Schneider", 9999999, 700 },
{“Hershey’s", “Michele Buck", 99999, 300 },
};
print_struct(companies);
return 0;
}
void print_struct(struct company str_arr[])
{
int i;
for(i= 0; i<3; i++)
{
printf("Name: %s\n", str_arr[i].name);
printf("CEO: %d\n", str_arr[i].ceo);
printf("Revenue: %.2f\n", str_arr[i].revenue);
printf("Price per stock : %.2f\n", str_arr[i].pps);
printf("\n");
}
}

You might also like