learneducのブログ

LearnEduc: Your path to excellence in learning. Elevate your knowledge journey today

Structure and Union in C Programming


Structure


Structures help programmers to group elements of different data types into a single logical unit, unlike arrays which permit a programmer to group only elements of the same data type.


A structure is a user-defined data type in C that allows you to combine different data types to store a particular type of record. It helps construct a complex data type in a more meaningful way. It is somewhat similar to an Array, but the key difference is that an array is used to store a collection of similar data types, while a structure can store a collection of any type of data.


Defining a Structure


The structkeyword is used to define a structure. structdefines a new data type which is a collection of different types of data.


Syntax:

struct structure_name
{
// declaration of different data types
};


Structure name is also called a structure tag. Note that the closing braces in the structure type declaration must be followed by a semicolon ( ;).


Example of Structure:

struct Book
{
char name[15];
int price;
int pages;
};


Here, Bookis the structure name/tag. name, price, and pagesare structure members.


Declaring Structure Variables:


Structure variables can be declared after the structure is defined. Structure variable declaration is similar to the declaration of variables of any other data type. Structure variables can be declared in two ways:


  1. Declaring structure variables separately:
struct Student
{
char name[20];
int age;
int rollno;
} S1, S2;

struct Student S3, S4;


S1, S2, S3, S4are structural variables.


  1. Accessing Data Members of Structure:


There are two ways to access the data member of a structure: using the dot operator ( .) and the arrow operator ( ->).


Dot Operator:

struct Student
{
char name[20];
int age;
int rollno;
int marks1, marks2, total_marks;
} S1, S2;

// Accessing data member of S1
S1.age = 20;
S1.total_marks = S1.marks1 + S1.marks2;


Arrow Operator:

struct Student
{
char name[20];
int age;
int rollno;
int marks1, marks2, total_marks;
} S1, S2;

struct Student *ptr;

ptr = &S1;

ptr->total_marks = ptr->marks1 + ptr->marks2;


Structure without Tag:


Structures can be defined without a tag.

struct
{
char name[20];
int age;
int rollno;
int marks1, marks2, total_marks;
} S1, S2;


Here, we can only create structure variables with the definition, as S1and S2have been created. We cannot create structure variables later in the program because there is no tag.


Nested Structure:


When a structure has another structure variable as its member, such structures are called nested structures.

struct date
{
int dd, mm, yy;
};

struct emp
{
char name[30];
int empcode;
struct date date_of_birth;
struct date date_of_joining;
};


Self-Referential Structure:


When a structure has a pointer to its own type of structure, such structure definitions are called self-referential structures.

struct list
{
int element_val;
struct list *previous, *next;
};


Here, previousand nextare pointers to the struct listtype element.


Union


A union is a special data type available in C that allows storing different data types in the same memory location. Only one member can contain a value at any given time.


Defining a Union

union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];


The union tag is optional, and each member definition is a normal variable definition, such as int i;or float f;or any other valid variable definition.


At the end of the union's definition, before the final semicolon, you can specify one or more union variables, but it is optional.


Example:

union Data
{
int i;
float f;
char str[20];
} data;


Command Line Arguments


In C programs, command-line arguments can be passed to the program through the command prompt. These arguments are accessed using two predefined variables: argcand argv.


argc represents the count of arguments, and argv is a pointer to the argument array. The first argument is the name of the program.


Example:

#include 

int main(int argc, char *argv[])
{
int i;
printf("Number of arguments: %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}


This program prints the number of arguments passed to it along with their values.