Unit 4 Final Notes
Unit 4 Final Notes
❮ PreviousNext ❯
Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
For example: If I have to write a program to store Student information, which will have
Student's name, age, branch, permanent address, father's name etc, which included
string values, integer values etc, how can I use arrays for this problem, I will require
something which can hold data of different types together.
Defining a structure
struct keyword is used to define a structure. struct defines a new data type which is a
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
struct structName
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
};
Example:
Use the struct keyword inside the main() method, followed by the name of
the structure and then the name of the structure variable:
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
Example
// Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
}
Now you can easily create multiple structure variables with different values,
using just one structure:
Example
// Create different struct variables
struct myStructure s1;
struct myStructure s2;
s2.myNum = 20;
s2.myLetter = 'C';
Example2:
1. struct employee
2. { int id;
3. char name[50];
4. float salary;
5. };
The variables e1 and e2 can be used to access the values stored in the structure.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
1. struct employee
2. { int id;
3. char name[50];
4. float salary;
5. }e1,e2;
1. #include<stdio.h>
2. #include <string.h>
3. struct employee
4. { int id;
5. char name[50];
6. }e1; //declaring e1 variable for structure
7. int main( )
8. {
9. //store first employee information
10. e1.id=101;
11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
12. //printing first employee information
13. printf( "employee 1 id : %d\n", e1.id);
14. printf( "employee 1 name : %s\n", e1.name);
15. return 0;
16. }
1. #include<stdio.h>
2. #include <string.h>
3. struct employee
4. { int id;
5. char name[50];
6. float salary;
7. }e1,e2; //declaring e1 and e2 variables for structure
8. int main( )
9. {
10. //store first employee information
11. e1.id=101;
12. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
13. e1.salary=56000;
14. //store second employee information
15. e2.id=102;
16. strcpy(e2.name, "James Bond");
17. e2.salary=126000;
18. //printing first employee information
19. printf( "employee 1 id : %d\n", e1.id);
20. printf( "employee 1 name : %s\n", e1.name);
21. printf( "employee 1 salary : %f\n", e1.salary);
22. //printing second employee information
23. printf( "employee 2 id : %d\n", e2.id);
24. printf( "employee 2 name : %s\n", e2.name);
25. printf( "employee 2 salary : %f\n", e2.salary);
26. return 0;
27. }
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
return 0;
}
Output:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
C Array of Structures-
Consider a case, where we need to store the data of 5 students. We can store it by using the
structure as given below.
1. #include<stdio.h>
2. struct student
3. {
4. char name[20];
5. int id;
6. float marks;
7. };
8. void main()
9. {
10. struct student s1,s2,s3;
11. int dummy;
12. printf("Enter the name, id, and marks of student 1 ");
13. scanf("%s %d %f",s1.name,&s1.id,&s1.marks);
14. scanf("%c",&dummy);
15. printf("Enter the name, id, and marks of student 2 ");
16. scanf("%s %d %f",s2.name,&s2.id,&s2.marks);
17. scanf("%c",&dummy);
18. printf("Enter the name, id, and marks of student 3 ");
19. scanf("%s %d %f",s3.name,&s3.id,&s3.marks);
20. scanf("%c",&dummy);
21. printf("Printing the details....\n");
22. printf("%s %d %f\n",s1.name,s1.id,s1.marks);
23. printf("%s %d %f\n",s2.name,s2.id,s2.marks);
24. printf("%s %d %f\n",s3.name,s3.id,s3.marks);
25. }
Output
Array of Structures in C-
Let's see an example of an array of structures that stores information of 5 students and prints
it.
1. #include<stdio.h>
2. #include <string.h>
3. struct student{
4. int rollno;
5. char name[10];
6. };
7. int main(){
8. int i;
9. struct student st[5];
10. printf("Enter Records of 5 students");
11. for(i=0;i<5;i++){
12. printf("\nEnter Rollno:");
13. scanf("%d",&st[i].rollno);
14. printf("\nEnter Name:");
15. scanf("%s",&st[i].name);
16. }
17. printf("\nStudent Information List:");
18. for(i=0;i<5;i++){
19. printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
20. }
21. return 0;
22. }
Output:
Pointer Syntax
int *p
int *p, c ;
c = 5;
p = &c;
printf(“%d”, *p);
Output: 5
Here, the address of c is assigned to the pc pointer. To get the value stored in that address, we
used *pc .
int *pc, c;
c = 5;
pc = &c;
printf(“%d”,c);
printf(“%d”,*pc);
We have assigned the address of c to the pc pointer.
Then, we changed the value of c to 1. Since pc and the address of c is the
same, *pc gives us 1.
Let's take another example.
int *pc, c;
c = 5;
*pc = &c;
*pc = 1;
printf(“%d”, *pc);
printf(“%d”,c);
We have assigned the address of c to the pc pointer.
Then, we changed *pc to 1 using *pc = 1; . Since pc and the address of c is the
same, c will be equal to 1.
Let's take one more example.
int *pc , c , d;
c = 5;
d = 15;
pc = &c;
printf(“%d”, *pc);
pc = &d);
printf(“%d”, *pc);
Example: Working of Pointers
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
This assigns 22 to the variable c. That is, 22 is stored in the memory location of
variable c.
3. pc = &c;
4. c = 11;
5. *pc = 2;
This change the value at the memory location pointed by the pointer pc to 2.
#include <stdio.h>
int main()
{
//Variable declaration
int num = 10;
//Pointer declaration
int *p;
double a = 10;
double *p;
p = &a;
*p would give us the value of the variable a. The following statement would display 10 as
output.
printf("%d", *p);
Similarly if we assign a value to *pointer like this:
*p = 200;
It would change the value of variable a. The statement above will change the value of a
from 10 to 200.
#include <stdio.h>
int main()
{
int var =10;
int *p;
p= &var;
return 0;
}
Output:
It is the most common. Each node has data and a pointer to the next node.
Singly linked list
struct node {
int data;
struct node *next;
}
A node is represented as
struct node {
int data;
struct node *next;
struct node *prev;
}
A circular linked list is a variation of a linked list in which the last element is linked to
the first element. This forms a circular loop.
Circular linked list
A circular linked list can be either singly linked or doubly linked.
• for singly linked list, next pointer of last item points to the first item
• In the doubly linked list, prev pointer of the first item points to the last item as
well.
A circular doubly linked list is a mixture of a doubly linked list and a circular linked
list. Like the doubly linked list, it has an extra pointer called the previous pointer, and
similar to the circular linked list, its last node points at the head node. This type of
linked list is the bi-directional list. So, you can traverse it in both directions.