Lab 01 Revision of C Concepts
Lab 01 Revision of C Concepts
Objectives:
Defining a Structure:
Before we can create structure variables, we need to define its data type. To define a structure, the
struct keyword is used.
Here is an Example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Here, a derived type struct Person is defined. Now, we can create variables of this type.
When a struct type is declared, no storage or memory is allocated. To allocate memory of a given
structure type and work with it, we need to create variables.
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1, person2, p[20];
return 0;
}
Another way of creating a struct variable is:
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
In both cases, two variables person1, person2, and an array variable p having 20 elements of type
struct Person are created.
There are two types of operators used for accessing members of a structure.
Suppose, we want to access the salary of person2. Here's how we can do it.
person2.salary
C Pointers to structures:
struct name
{
member1;
member2;
.
.
};
int main()
{
struct name *ptr;
}
Here, a pointer ptr of type struct name is created. That is, ptr is a pointer to struct.
Access members using Pointer:
To access members of a structure using pointers, we use the Arrow “->” operator.
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr>age);
printf("Enter weight: ");
scanf("%f", &personPtr>weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr>age);
printf("weight: %f", personPtr>weight);
return 0;
}
In this example, the address of person1 is stored in the personPtr pointer using
personPtr = &person1;.
Now, we can access the members of person1 using the personPtr pointer
Dynamic Memory Allocation for structures:
Before you proceed with this section, we recommend you to check C dynamic memory allocation in
your programming text book.
Sometimes, the number of struct variables we declared may be insufficient. We may need to
allocate memory during run-time. Here's how we can achieve this in C programming.
Enter the number of persons: 2
Enter first name and age respectively: Harry 24
Enter first name and age respectively: Gary 32
Displaying Information:
Name: Harry Age: 24
Name: Gary Age: 32
In the above example, n number of struct variables are created where n is entered by the user.
ptr = (struct person*) malloc(n * sizeof(struct person));
In-Lab Task 2:
The program in Code Listing 1 writes a single record to the file. Modify it, to use a function
‘write_records_to_file’ with following prototype:
int write_records_to_file (struct emp * sptr, int num_records, FILE * fptr)
This function should write ’num_records’’ number of structures from a dynamically allocated
memory pointed to by ‘sptr’ to a file pointed to by ‘fptr’. It should return the number of
structures successfully written to the file.
In-Lab Task 3:
int read_records_from_file(struct emp * sptr, int num_records, FILE * fptr)
void print_records(struct emp * sptr, int num_records);
Use these functions to read a previously written file (employees_records2.dat) and display the
contents on screen.
Post-Lab Task:
The structures that you write in a file using ‘fwrite()’ are written in the binary format and cannot be
viewed in a text editor properly. Your task is to write the contents of these structures in the text
format so that the contents may be viewed in a text editor.
#include <stdio.h>
#include <stdlib.h>
/// Define a structure 'emp' to hold data about an employee
struct emp
{
char name[48]; // Name of the employee
int age;
float bs; // Basic Salary as a floating point number.
};
void flush(void);
int write_records_to_file (struct emp * sptr, int num_records, FILE * fptr);
int read_records_from_file(struct emp * sptr, int num_records, FILE * fptr);
void print_records(struct emp * sptr, int num_records);
int main(void)
{
FILE *fp ;
char another = 'Y' ;
struct emp employee;
int i = 0;
int num_rec = 0;
// Open the file for writing in the Binary Mode
fp = fopen ( "employees_records.dat", "wb" ) ;
if ( fp == NULL ){
printf ( "Cannot open file\n" ) ;
exit(0) ;
}
while ( another == 'Y' )
{
printf ( "\nEnter the name of the Employee: " ) ;
fgets (employee.name, 48, stdin);
printf ( "\nEnter the age of the Employee: " ) ;
scanf("%d", &employee.age);
printf ( "\nEnter the Basic Salary of the Employee: " ) ;
scanf("%f", &employee.bs ) ;
// Writing to file
fwrite ( &employee, sizeof ( struct emp ), 1, fp );
flush();
printf ( "Add another record (Y/N) " );
another = getchar() ;
flush();
i++;
}
fclose(fp);
return(0);
}
Code Listing 1