0% found this document useful (0 votes)
9 views24 pages

CH 5-1

The document provides a comprehensive overview of structures in C programming, including syntax for defining structures, declaring structure variables, and accessing structure members. It also covers nested structures, arrays of structures, and the use of structures with functions. Additionally, it discusses dynamic memory allocation for structures and file operations related to structure data.

Uploaded by

vidya swami
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)
9 views24 pages

CH 5-1

The document provides a comprehensive overview of structures in C programming, including syntax for defining structures, declaring structure variables, and accessing structure members. It also covers nested structures, arrays of structures, and the use of structures with functions. Additionally, it discusses dynamic memory allocation for structures and file operations related to structure data.

Uploaded by

vidya swami
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/ 24

05 Structures

Syntax-

struct structure_Name
{
dataType member1;
dataType member2;
...
};
Structure Definition

2. Structure Variable Declaration after Structure


1. Structure Variable Declaration with Structure Template
Template
struct structure_name // structure declared beforehand
{
data_type member_name1; struct structure_name variable1, variable2, .......;
data_type member_name1;
....
struct employee
....
}variable1, varaible2, ...; { int id;
char name[50];

struct employee float salary;

{ int id; };

char name[50];
float salary; struct employee e1, e2;
}e1,e2;
Initialize Structure Members
Accessing members of the structure
1. Initialization using Assignment Operator
struct structure_name str;
str.member1 = value1;
There are two ways to access structure members: str.member2 = value2;
str.member3 = value3;
By . (member or dot operator) .
By -> (structure pointer operator) .
.
Syntax-
2. Initialization using Initializer List
struct structure_name str = { value1, value2, value3 };
structure_name.member1;
strcuture_name.member2; 3. Initialization using Designated Initializer List

struct structure_name str = { .member1 = value1,


.member2 = value2, .member3 = value3 };
#include <stdio.h>
struct book
{
char title[10];
char author[20];
float price;
int pages;
};

void main()
{
struct book book1 = {"Let Us C", "Yashavant Kanetkar", 355, 508};

printf("Title: %s \n", b1.title);


printf("Author: %s \n", b1.author);
printf("Price: %lf\n", b1.price);
printf("Pages: %d \n", b1.pages);
getch();
}
# include <stdio.h>
int main( )
{
struct book
{
char name ;
float price ;
int pages ;
};
struct book b1, b2, b3 ;
printf (“\n Enter names, prices & no. of pages of 3 books");
scanf ( "%c %f %d", &b1.name, &b1.price, &b1.pages ) ;
scanf ( "%c %f %d", &b2.name, &b2.price, &b2.pages ) ;
scanf ( "%c %f %d", &b3.name, &b3.price, &b3.pages ) ;
printf ( “Book Details\n" ) ;
printf ( "%c %f %d\n", b1.name, b1.price, b1.pages ) ;
printf ( "%c %f %d\n", b2.name, b2.price, b2.pages ) ;
printf ( "%c %f %d\n", b3.name, b3.price, b3.pages ) ;
return 0 ;
}
Nested Structures
#include<stdio.h>
struct address printf(“\n Enter City: ”);
{
int id; scanf(“%s”, emp.add.city);
char city[20]; printf(“\n Enter Phone : ”);
int ph;
}; scanf(“%d”,&emp.add.ph);
struct employee
{
char name[20]; printf(“\n Name of employee : %s”, emp.name);
struct address add;
}; printf(“\n Employee Id: %d”, emp.add.id);
void main () printf(“\n Enter City: %s”, emp.add.city );
{
struct employee emp; printf(“\n Phone: %d”,emp.add.phone);
}
printf(“\n Enter employee name: ”);
scanf(“%s”,&emp.name);
printf(“\n Enter id : ”);
scanf(“%d”,&emp.add.id);
Array of Structures
#include<stdio.h>
#include<conio.h> for(n=0;n<=2;n++)
void main() {
{ printf("\n-----------------------------------------------------\n");
struct book
{ printf("\n Name of Book_%d name:%s ",n+1,b[n].bname);
char bname[50]; printf("\n Number of pages of Book_%d
int page;
float price; Pages:%d",n+1,b[n].page);
}; printf("\n Price of Book_%d price:%f",n+1,b[n].price);
struct book b[3]; }
int n;
clrscr(); getch();
for(n=0;n<=2;n++) }
{
printf("\n Enter Book_%d name: ",n+1); void linkfloat()
scanf("%s",&b[n].bname); {
float a=0,*b;
printf("\n Enter Book_%d Pages: ",n+1); b=&a;
scanf("%d",&b[n].page); a=*b;
}
printf("\n Enter Book_%d price: ",n+1);
scanf("%f",&b[n].price);
}
#include<stdio.h>
#include <string.h>
struct student printf("\nStudent Information List:");
{ for(m=0;m<3;m++)
int rollno; {
char name[10]; printf("\nRollno:%d\tName:%s\tPercentage:0.2f",st[m].rollno,
float per; st[m].name,st[m].per);
}; }
void main() getch();
{ }
int m;
struct student st[5];
clrscr(); void linkfloat()
printf("Enter Records of 3 students"); {
for(m=0;m<3;m++) float a=0,*b;
{ b=&a;
printf("\nEnter Rollno:"); a=*b;
scanf("%d",&st[m].rollno); }
printf("\nEnter Name:");
scanf("%s",&st[m].name);
printf("\n Enter percentage: ");
scanf("%f",&st[m].per);
}
Structure and Function
#include <stdio.h>
#include <string.h>
struct student void display(struct student s)
{ {
char name[50]; printf("\n Displaying information");
int age; printf(“\n Name: %s", s.name);
}; printf("\n Age: %d", s.age);
}
void display(struct student s);
void main()
{
struct student s1;
printf(“\n Enter name of the student: ");
scanf("%s", s1.name);
printf(“\n Enter age of the student: ");
scanf("%d", &s1.age);
display(s1); // passing struct as an argument

getch();
}
#include <stdio.h>
#include <string.h> struct student getInformation();
struct student
{ {
char name[50];
struct student s1;
int age;
}; printf(“\n Enter name of the student: ");
scanf("%s", s1.name);
struct student getInformation();
void main() printf(“\n Enter age of the student: ");
{
scanf("%d", &s1.age);
struct student s;
s = getInformation(); return s1;
}
printf("\n Displaying information");
printf(“\n Name: %s", s.name);
printf("\n Age: %d", s.age);
getch();
}
Copying of Structure Elements
# include <stdio.h>
void main( )
{
struct employee
{
char name[ 10 ] ; int age ; float salary ;
};
struct employee e1 = { "Sanjay", 30, 5500.50 } ;
struct employee e2, e3 ;
strcpy ( e2.name, e1.name ) ; /* e2.name = e1. name is wrong */
e2.age = e1.age ;
e2.salary = e1.salary ;
/* copying all elements at one go */
e3 = e2 ;
printf ( "%s %d %f\n", e1.name, e1.age, e1.salary ) ;
printf ( "%s %d %f\n", e2.name, e2.age, e2.salary ) ;
printf ( "%s %d %f\n", e3.name, e3.age, e3.salary ) ;
getch() ;
}
Structure and Pointers
struct name
{
member1;
member2;
.
.
};

int main()
{
struct name *ptr;
}
Example: Access members using Pointer
#include <stdio.h> printf(“\n Displaying: ");

struct person printf("Age: %d\n", ptr->age);

{ int age; printf("weight: %f", ptr->weight);

float weight; getch();

}; }

void main()
{
struct person *ptr, p1;
ptr = &p1;
printf(“\n Enter age: ");
scanf("%d", &ptr->age);
printf("Enter weight: ");
scanf("%f", &ptr->weight);
Dynamic memory allocation of structs
for(i = 0; i < n; ++i)
{
#include <stdio.h>
printf("Enter first name and age respectively: ");
#include <stdlib.h>
scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
struct person
}
{
int age;
printf(“\nDisplaying Information:");
float weight;
for(i = 0; i < n; ++i)
char name[30];
{
};
printf("Name: %s\tAge: %d\n", (ptr+i)->name,
(ptr+i)->age);
int main()
}
{
return 0;
int i, n; struct person *ptr;
}

printf("Enter the number of persons: ");


scanf("%d", &n);

// allocating memory for n numbers of struct person

ptr = (struct person*) malloc(n * sizeof(struct person));


typedef with structures
struct student
{
char name[20]; typedef struct student
int age; {
}; char name[20];
struct student s1; int age;
} stud;
stud s1,s2;
struct student
{
char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;
typedef with structures
#include <stdio.h> printf("\nEnter the age of student:");

typedef struct student scanf("%d",&s1.age);

{ printf("\n Name of the student is : %s", s1.name);

char name[20]; printf("\n Age of the student is : %d", s1.age);

int age; return 0;

}stud; }

int main()
{
stud s1;
printf("Enter the details of student s1: ");
printf("\nEnter the name of the student:");
scanf("%s",&s1.name);
sprintf( ) and sscanf( ) Functions

# include <stdio.h>
int main( )
{
int i = 10 ;
char ch = 'A' ;
float a = 3.14 ;
char str[ 20 ] ;
printf ( "%d %c %f\n", i, ch, a ) ;
sprintf ( str, "%d %c %f", i, ch, a ) ;
printf ( "%s\n", str ) ;
return 0 ;
printf ( "format string", list of variables ) ;
File Operations
There are different operations that can be carried out on a file.
These
are:
(a) Creation of a new file
(b) Opening an existing file
(c) Reading from a file
(d) Writing to a file
(e) Moving to a specific location in a file (seeking)
(f) Closing a file
# include <stdio.h>
int main( )
{
FILE *fp ;
char ch ;
clrscr();
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf ( "%c", ch ) ;
}
printf ( "\n" ) ;
fclose ( fp ) ;
return 0 ;
//getch();
}

You might also like