C – Structure

Prev     Next

C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.

  • If you want to access structure members in C, structure variable should be declared.
  • Many structure variables can be declared for same structure and memory will be allocated for each separately.
  • It is a best practice to initialize a structure to null while declaring, if we don’t assign any values to structure members.

Difference between C variable, C array and C structure:

  • A normal C variable can hold only one data of one data type at a time.
  • An array can hold group of data of same data type.
  • A structure can hold group of data of different data types and Data types can be int, char, float, double and long double etc.

C Structure:

Syntax struct student
{
int a;
char b[10];
}
Example a = 10;
b = “Hello”;

C Variable:

int Syntax: int a;
Example: a = 20;
char Syntax: char b;
Example: b=’Z’;

C Array:

int Syntax: int a[3];
Example:
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = ‘\0’;
char Syntax: char b[10];
Example:
b=”Hello”;

Below table explains following concepts in C structure.

  1. How to declare a C structure?
  2. How to initialize a C structure?
  3. How to access the members of a C structure?
Using normal variable Using pointer variable
Syntax:
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Syntax:
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example:
struct student
{
int  mark;
char name[10];
float average;
};
Example:
struct student
{
int  mark;
char name[10];
float average;
};
Declaring structure using normal variable:
struct student report;
Declaring structure using pointer variable:
struct student *report, rep;
Initializing structure using normal variable:
struct student report = {100, “Mani”, 99.5};
Initializing structure using pointer variable:
struct student rep = {100, “Mani”, 99.5};
report = &rep;
Accessing structure members using normal variable:
report.mark;
report.name;
report.average;
Accessing structure members using pointer variable:
report  -> mark;
report -> name;
report -> average;

Example program for C structure:

This program is used to store and access “id, name and percentage” for one student. We can also store and access these data for many students using array of structures. You can check “C – Array of Structures” to know how to store and access these data for many students.

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program – Another way of declaring C structure:

In this program, structure variable “record” is declared while declaring structure itself. In above structure example program, structure variable “struct student record” is declared inside main function which is after declaring structure.

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

C structure declaration in separate header file:

In above structure programs, C structure is declared in main source file. Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.

Header file name – structure.h

Before compiling and executing below C program, create a file named “structure.h” and declare the below structure.

struct student
{
int id;
char name[20];
float percentage;
} record;

Main file name – structure.c:

In this program, above created header file is included in “structure.c” source file as #include “Structure.h”. So, the structure declared in “structure.h” file can be used in “structure.c” source file.

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Uses of structures in C:

  1. C Structures can be used to store huge data. Structures act as a database.
  2. C Structures can be used to send data to the printer.
  3. C Structures can interact with keyboard and mouse to store the data.
  4. C Structures can be used in drawing and floppy formatting.
  5. C Structures can be used to clear output screen contents.
  6. C Structures can be used to check computer’s memory size etc.

Continue on C – Array of Structures….

Continue on C – Passing structure to function….

Continue on C – Structure using Pointer….

Continue on C – Structure within Structure….

Continue on C – Structure Memory Allocation….

Continue on C – Structure Padding….

Prev     Next





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!