Structure, Union, Enumeration and File Concepts
Structure, Union, Enumeration and File Concepts
e
name[20]; int }
scanf("%f", &arr_student[i].marks);
Program
roll_no; float printf("\n");
marks; printf("Name\tRoll no\tMarks\n");
}; for(i = 0; i < 2; i++ )
int main() {
{ printf("%s\t%d\t%.2f\n", arr_student[i].name,
arr_student[i].roll_no,
struct student
arr_student[i].marks);
arr_student[2]; int i; }
return 0;
#include<stdio.h> Output:
struct address ******
{ Enter employee information Name/ City/ PIN Code/
char Phone Number?
city[20]; int Kani
pin; Madurai
char 600009
999411
phone[14];
7284
Write a program }; Printing the employee information....
to read and struct employee name: Kani
display { City: Madurai
information of an
char name[20]; Pincode: 600009
employee using
structure within a struct address Phone:
add; 9994117284
structure
};
void
printf("Enter
main () employee information Name/ City/ PIN Code/ Phone Number?\
{ n"); scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin,
emp.add.phone);
struct employee printf("Printing the employee information....\n");
printf("name:
emp; %s\nCity: %s\nPincode: %d\nPhone:
%s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
#include<stdio.h>
typedef struct
{
int x;
int
y;
}POINT;
void
Passing display(POI
Structure NT);
void main()
as {
arguments POI
NT
p1 =
{2,
3};
displ
#include<stdio.h
> struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
main()
{ struct student stud1, *ptr_stud1;
Accessing ptr_stud1 = &stud1;
ptr_stud1->r_no = 01;
Structure strcpy(ptr_stud1->name, "Rahul");
using strcpy(ptr_stud1->course,
"BCA"); ptr_stud1->fees = 45000;
Pointer printf("\n DETAILS OF
STUDENT");
printf("\n -");
printf("\n ROLL NUMBER = %d", ptr_stud1->r_no);
printf("\n NAME =%s ", puts(ptr_stud1->name));
printf("\n COURSE = %s", puts(ptr_stud1->course));
} printf("\n FEES = %f", ptr_stud1->fees);
Self Referential Structures: contain reference to
data of its same type
struct Node
{
int data;
struct Node *next;
};
Dynamic data structures : Structures with pointer members that
refer to the same structure.
• Arrays and other simple variables are allocated at block
entry.
• But dynamic data structures require storage management
routine to explicitly obtain and release memory.
int main()
{
// A simple C program for traversal of a linked list struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
#include <stdio.h>
#include <stdlib.h> // allocate 3 nodes in the heap
head = (struct Node*)malloc(sizeof(struct Node));
struct Node { second = (struct Node*)malloc(sizeof(struct Node));
int data; third = (struct Node*)malloc(sizeof(struct Node));
struct Node* next;
}; head->data = 1; // assign data in first node
head->next = second; // Link first node with second