0% found this document useful (0 votes)
2 views

Structure Programs

The document contains multiple C programs demonstrating the use of structures, unions, and pointers. It includes examples of accessing structure members, initializing structures, creating dynamic arrays of structures, and using self-referential structures for linked lists. Additionally, it compares structures and unions, highlighting their differences in memory allocation and data access.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Structure Programs

The document contains multiple C programs demonstrating the use of structures, unions, and pointers. It includes examples of accessing structure members, initializing structures, creating dynamic arrays of structures, and using self-referential structures for linked lists. Additionally, it compares structures and unions, highlighting their differences in memory allocation and data access.

Uploaded by

colabpython39
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

C program to demonstrate how to access

structure members using pointer variable:


#include<stdio.h>
#include<malloc.h>
struct emp {
int eid; char name[10];
}*ptr;
int main() {
printf("Enter the Employee Details : ");
ptr = (struct emp *) malloc(sizeof(struct
emp));
printf("\nEnter the Employee ID : ");
scanf("%d", &ptr->eid);
printf("\nEnter the Employee Name : ");
scanf("%s", ptr->name);
printf("\nEmployee Details are : ");
printf("\nEmployee ID: %d", ptr->eid);
printf("\nEmployee Name : %s", ptr->name);
free(ptr);
return 0;
}

C program to demonstrate how to initialize


structure members and assign address of
normal variable to pointer variable:
#include<stdio.h>
#include<string.h>
struct text_book {
char author_name[20];
char book_name[50];
int cost;
};
int main() {
struct text_book a;
struct text_book *ptr;
strcpy(a.author_name, “Dennis ritchie”);
strcpy(a.book_name, “The C Programming
Language”);
a.cost=300;
ptr = &a;
printf(“Book Details:\n”);
printf(“Author Name = %s\n”, ptr-
>author_name);
printf(“Book Name = %s\n”, ptr-
>book_name);
printf(“Cost of the Book = %d\n”, ptr->cost);
return 0;
}

Declare a calendar as an array of 7 elements


(A dynamically Created array) to represent 7
days of a week. Each Element of the array is
a structure having three fields. The first field
is the name of the Day (A dynamically
allocated String), The second field is the date
of the Day (A integer), the third field is the
description of the activity for a particular
day (A dynamically allocated String).
The strdup() function creates a duplicate of the
string pointed to by src , and returns a pointer to
the new copy. The strdup() function allocates
the memory for the new string by calling
malloc(); it's up to you to release the memory by
calling free().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a structure to represent a day's
activity
struct DayActivity {
char *dayName;
int date;
char *activityDescription;
};
// Function to create a day's activity
struct DayActivity createDayActivity(const
char* dayName, int date, const char*
activityDescription) {
struct DayActivity day;
day.dayName = strdup(dayName);
day.date = date;
day.activityDescription =
strdup(activityDescription);
return day;
}
// Function to read a day's activity
void readDayActivity(const struct DayActivity
*day) {
printf("Day: %s\n", day->dayName);
printf("Date: %d\n", day->date);
printf("Activity: %s\n", day-
>activityDescription);
}
// Function to free the memory allocated for a
day's activity
void freeDayActivity(struct DayActivity *day)
{
free(day->dayName);
free(day->activityDescription);
}
int main() {
// Create an array of 7 DayActivity structures
struct DayActivity calendar[7];
// Initialize each element of the calendar
array
calendar[0] = createDayActivity("Monday",
1, "Work");
calendar[1] = createDayActivity("Tuesday",
2, "Gym");
calendar[2] =
createDayActivity("Wednesday", 3, "Meeting");
calendar[3] = createDayActivity("Thursday",
4, "Shopping");
calendar[4] = createDayActivity("Friday", 5,
"Lunch with friends");
calendar[5] = createDayActivity("Saturday",
6, "Hiking");
calendar[6] = createDayActivity("Sunday", 7,
"Relax");
// Read and display the day activities
for (int i = 0; i < 7; i++) {
readDayActivity(&calendar[i]);
}
// Free the allocated memory
for (int i = 0; i < 7; i++) {
freeDayActivity(&calendar[i]);
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DAYS_IN_WEEK 7
// Define the structure for a calendar day
struct CalendarDay {
char *dayName;
int date;
char *activityDescription;
};
int main() {
// Create an array of CalendarDay structures
struct CalendarDay
week[DAYS_IN_WEEK];
// Initialize the calendar with data for each
day
for (int i = 0; i < DAYS_IN_WEEK; i++) {
week[i].dayName = (char *)malloc(20 *
sizeof(char)); // Allocating memory for
dayName
week[i].activityDescription = (char
*)malloc(100 * sizeof(char)); // Allocating
memory for activityDescription
// Example data initialization
switch (i) {
case 0:
strcpy(week[i].dayName, "Monday");
week[i].date = 1;
strcpy(week[i].activityDescription,
"Work");
break;
case 1:
strcpy(week[i].dayName, "Tuesday");
week[i].date = 2;
strcpy(week[i].activityDescription,
"Gym");
break;
case 2:
strcpy(week[i].dayName,
"Wednesday");
week[i].date = 3;
strcpy(week[i].activityDescription,
"Meeting");
break;
case 3:
strcpy(week[i].dayName,
"Thrusday");
week[i].date = 4;
strcpy(week[i].activityDescription,
"Shopping");
break;
case 4:
strcpy(week[i].dayName, "Friday");
week[i].date = 5;
strcpy(week[i].activityDescription,
"Lunch with friends");
break;
case 5:
strcpy(week[i].dayName, "Saturday");
week[i].date = 5;
strcpy(week[i].activityDescription,
"Hiking");
break;
case 6:
strcpy(week[i].dayName, "Sunday");
week[i].date = 5;
strcpy(week[i].activityDescription,
"Relax");
break;
default:
break;
}
}
// Access and print the calendar
for (int i = 0; i < DAYS_IN_WEEK; i++) {
printf("%s (Date %d): %s\n",
week[i].dayName, week[i].date,
week[i].activityDescription);
}
// Free allocated memory
for (int i = 0; i < DAYS_IN_WEEK; i++) {
free(week[i].dayName);
free(week[i].activityDescription);
}
return 0;
}

Write appropriate structure definition and


variable declarations to store and display
following information about 50 students:
Name, USN, Gender, DOB and Marks in
three subjects S1, S2 and S3, Date of birth
should be a structure containing fields day,
month and year.
#include<stdio.h>
struct DOB {
int dd, mm, yy;
};
struct stud {
char name[20], usn[10]; char gender; struct
DOB dob; int s1, s2, s3;
};
int main() {
struct stud s[50];
int i, n;
printf("Enter number of students\n");
scanf("%d", &n);
printf("Enter %d details\n", n);
for(i=0; i<n; i++) {
printf("Enter %d student information such
as Name, USN, Gender, marks of three subjects
and date of birth\n", i+1);
scanf("%s %s %c %d %d %d %d %d %d",
&s[i].name, &s[i].usn, &s[i].gender, &s[i].s1,
&s[i].s2, &s[i].s3, &s[i].dob.dd, &s[i].dob.mm,
&s[i].dob.yy);
}
printf("Students Information:\n");
for(i=0; i<n; i++) {
printf("%s %s %c %d %d %d %d %d
%d", s[i].name, s[i].usn, s[i].gender, s[i].s1,
s[i].s2, s[i].s3, s[i].dob.dd, s[i].dob.mm,
s[i].dob.yy);
}
return 0;
}

Write a C program with an appropriate


structure definition and variable declaration
to read and display information about 20
employees using nested structures. Consider
the following fields like Ename, Empid, DOJ
(Date, Month, Year) and Salary (Basic, DA,
HRA).
#include<stdio.h>
struct DOB {
int mm, dd, yy;
};
struct Salary {
int Basic, DA, HRA;
};
struct Emp {
char name[20]; char Empid[10]; struct
DOB dob; struct Salary sal;
};
int main() {
struct Emp E[20];
int i, n;
printf("Enter the number of employees\n");
scanf("%d", &n);
printf("Enter %d employees information such
as name, emp-id, dob, sal(Basic, HRA, DA)\n",
n);
for(i=0; i<n; i++) {
scanf("%s %s %d %d %d %d %d %d",
&E[i].name, &E[i].Empid, &E[i].dob.dd,
&E[i].dob.mm, &E[i].dob.yy,&E[i].sal.Basic,
&E[i].sal.DA, &E[i].sal.HRA);
}
printf("%d Employee Information Details:\n",
n);
for(i=0; i<n; i++) {
printf("%s %s %d/%d/%d %d %d %d\n",
E[i].name, E[i].Empid, E[i].dob.dd,
E[i].dob.mm, E[i].dob.yy, E[i].sal.Basic,
E[i].sal.DA, E[i].sal.HRA);
}
return 0;
}

A self-referential structure is a structure that


contains a pointer to a variable of the same type.
This allows the structure to refer to itself,
creating a linked data structure. The concept of
a self-referential structure is based on the idea of
a linked list, which is a collection of data
elements that are connected to each other
through a series of pointers. Each element in the
list contains a data value and a pointer to the
next element in the list. This creates a linked
structure where each element is connected to the
next one.
#include<stdio.h>
#include<alloc.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void main( ) {
struct node *head,*temp;
head=NULL;
//First Node
temp=(strcut node*)calloc(1,sizeof(struct
node));
printf("Enter data ");
scanf("%d", &temp->data);
temp->next=NULL;
head=temp;
}

C program to illustrate how to access


members of union using pointer variable:
#include<stdio.h>
#include<malloc.h>
union emp {
int eid;
char name[10];
}*ptr;
int main() {
printf("Enter the Employee Details : ");
ptr = (union emp *) malloc(sizeof(union
emp));
printf("\nEnter the Employee ID : ");
scanf("%d", &ptr->eid);
printf("\nEnter the Employee Name : ");
scanf("%s", ptr->name);
printf("\nEmployee Details are : ");
printf("\nEmployee ID: %d", ptr->eid);
printf("\nEmployee Name : %s", ptr->name);
return 0;
}

C program to illustrate how to initialize


members of union and assign address of
normal variable to pointer variable:
#include<stdio.h>
#include<string.h>
union text_book {
char author_name[20];
char book_name[50];
int cost;
};
int main() {
union text_book a;
union text_book *ptr;
strcpy(a.author_name, “Dennis ritchie”);
strcpy(a.book_name, “The C Programming
Language”);
a.cost=300;
ptr = &a;
printf(“Book Details:\n”);
printf(“Author Name = %s\n”, ptr-
>author_name);
printf(“Book Name = %s\n”, ptr-
>book_name);
printf(“Cost of the Book = %d\n”, ptr->cost);
return 0;
}

Structure Union
1. The keyword
1. The keyword struct is
union is used to
used to define structure.
define union.
2. It allocates piece
2. The compiler allocates
of memory that is
the memory for each
big enough to hold
member of the structure
the largest variable
variable.
of type in union.
3. Memory
3. Separate memory is allocated is shared
allocated for each member by individual
of the structure variable. members of the
union variable.
4. Changing the value of 4. Changing the
any of the structure value of any of the
member will not affect union member will
other members of modify other
the structure. member values.
5. At any time,
5. At any time,
programmer can
programmer can access all
access only one
the members of structure
member of union
variable.
variable.
6. Only the first or
6. All members of
any single member
structure variable can be
of union variable
initialized.
can be initialized.
Table: Differences between structure and
union

You might also like