CH 5-1
CH 5-1
Syntax-
struct structure_Name
{
dataType member1;
dataType member2;
...
};
Structure Definition
{ 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
void main()
{
struct book book1 = {"Let Us C", "Yashavant Kanetkar", 355, 508};
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: ");
}; }
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;
}
}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();
}