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

Introduction to Structures in c

The document introduces structures in C, explaining that they are user-defined data types that can combine various data types to store complex data in a meaningful way. It outlines the syntax for defining structures, creating variables, accessing members, and the use of the typedef keyword. Additionally, it covers file handling in C, detailing what a file is, how to process files, and the major operations that can be performed on files.

Uploaded by

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

Introduction to Structures in c

The document introduces structures in C, explaining that they are user-defined data types that can combine various data types to store complex data in a meaningful way. It outlines the syntax for defining structures, creating variables, accessing members, and the use of the typedef keyword. Additionally, it covers file handling in C, detailing what a file is, how to process files, and the major operations that can be performed on files.

Uploaded by

jrtadlip
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

INTRODUCTION TO STRUCTURES IN C

In simple words, a structure is a user-defined data


type in C. With structures we can combine various
data types to store a specific type of data. A
structure helps us to store a complex data type in
a more meaningful way. It is somewhat similar to
an array. The only difference is that an array is
used to store a collection of homogeneous data
types whereas a structure can store a collection of
heterogeneous types of data.
For example, we need to store the information
about students like his/her name, Roll number and
marks. We can store that information separately.
A structure gives us a way to store the collection
of that information in one unit under one name
since all that information is related to the student.

The keyword struct defines structures


Syntax of Structures
struct Structure_name
{
data_type member1;
data_type member2;
data_type memeber;
};
For example:

struct Person {
char name[50];
int citNo;
float salary;
};

>Here, a derived type struct Person is defined.


Now, you can create variables of this type.
Create struct Variables
When a struct type is declared, no storage or memory is allocated. T
allocate memory of a given structure type and work with it, we need
create variables.

struct Person {
// code
};
int main() {
struct Person person1, person2;
return 0;
}
Another way of creating a struct
variable is:

struct Person {
// code
} person1, person2;

In both cases,
• person1 and person2 are struct Person
variables
Access Members of a Structure
There are two types of operators used for accessing member
a structure.

1. (.) - Member operator


2. (->) - Structure pointer operator

Suppose, you want to access the salary of person2. Here’s


how you can do it

person2.salary
In this program, we have created a
struct named Person. We have also
created a variable of Person named
person1. In main(), we have assigned
values to the variables defined in
Person
for the person1 object.
strcpy(person1.name, "George
Orwell");
person1.citNo = 1984;
person1. salary = 2500;

Notice that we have used strcpy()


function to assign
the value to person1.name. This is
because name
is a char array (C-string) and we
cannot use the
The C language contains the
typedef struct keyword to allow
users to provide alternative names
for the primitive (e.g., int) and user-
defined (e.g struct) data types.
File Handling
What is a File?
• A file is a collection of related data that a
computer treats as a single unit.
• Computers store files to secondary storage
so that the contents of files remain
intact when a computer turns off.
• When a computer reads a file, it copies the file
from the storage device to memory; when it
writes to a file, it transfers data from memory to
the storage device.
• C uses a structure called FILE (defined in
stdio.h) to store the attributes of a file.
Steps in Processing a File
1. Create the stream via a pointer
variable using the FILE structure:
FILE *p;
2. Open the file, associating the stream
name with the file name.
3. Read or write the data.
4. Close the file.
Five major operations can be performed
on file are:
1. Creation of a new file.
2. Opening an existing file.
3. Reading data from a file.
4. Writing data in a file.
5. Closing a file
END

You might also like