Structure & Union

 Structure & Union

Structure definition :-

BCA 2022-23


In C language, a structure is a user-defined data type that groups together variables of different data types under a single name. Here's a short definition for a structure in C:

"A structure in C is a custom data type that combines variables of different data types into a single entity under a single name."

For example, the following code defines a simple structure named "Person" that contains two variables - "name" of type string and "age" of type int:

struct Person {
   char name[50];
   int age;
};

This structure can be used to create variables that represent people, like this:

struct Person person1;
person1.age = 25;
strcpy(person1.name, "John");

This allows you to store and manipulate related data as a single unit, making your code more organized and easier to understand.

giving values to members:-

In C language, you can assign values to the members of a structure using the dot (.) operator. Here's a short example:

Suppose you have a structure named "Person" that contains two members - "name" and "age". You can assign values to these members as follows:

struct Person {
   char name[50];
   int age;
};

int main() {
   struct Person person1;
   
   person1.age = 25;
   strcpy(person1.name, "John");
   
   return 0;
}

In the above example, we first define a structure named "Person" that contains two members - "name" of type char array and "age" of type int. Then, in the main function, we declare a variable "person1" of type "Person". We then assign values to the members of this variable using the dot (.) operator.

To assign the value 25 to the "age" member, we use the expression "person1.age = 25;". To assign the string "John" to the "name" member, we use the strcpy function to copy the string to the "name" member - "strcpy(person1.name, "John");".

structure initialization:-

In C language, you can initialize a structure when you declare it by providing values for its members in curly braces after the structure's name. Here's a short example:

Suppose you have a structure named "Person" that contains two members - "name" and "age". You can initialize this structure as follows:

struct Person {
   char name[50];
   int age;
};

int main() {
   struct Person person1 = {"John", 25};
   
   return 0;
}

In the above example, we first define a structure named "Person" that contains two members - "name" of type char array and "age" of type int. Then, in the main function, we declare a variable "person1" of type "Person" and initialize it with the values "John" and 25 for its "name" and "age" members, respectively.

Note that the order in which you provide the values must match the order in which the members are declared in the structure definition. Alternatively, you can also initialize the members of a structure individually using the dot (.) operator, as shown in the previous example.

Array of structure:-

In C language, you can create an array of structures by defining the structure and then declaring an array of that structure type. Here's a short example:

Suppose you have a structure named "Person" that contains two members - "name" and "age". You can create an array of "Person" structures as follows:


struct Person {
   char name[50];
   int age;
};

int main() {
   struct Person people[3];
   
   people[0].age = 25;
   strcpy(people[0].name, "John");
   
   people[1].age = 30;
   strcpy(people[1].name, "Mary");
   
   people[2].age = 20;
   strcpy(people[2].name, "Tom");
   
   return 0;
}

In the above example, we first define a structure named "Person" that contains two members - "name" of type char array and "age" of type int. Then, in the main function, we declare an array "people" of type "Person" with a size of 3.

We can then assign values to the members of each "Person" structure in the array using the dot (.) operator. For example, to assign the value 25 to the "age" member and the string "John" to the "name" member of the first "Person" structure in the array, we use the expressions "people[0].age = 25;" and "strcpy(people[0].name, "John");" respectively.

By using an array of structures, you can store multiple related data sets in a single variable and manipulate them as a group, making your code more efficient and organized.

structure within structure:-

In C language, you can define a structure which is a user-defined data type that groups together variables of different data types under a single name. You can then use this structure to create variables that contain the group of variables defined in the structure.

For example, consider the following structure definition:

struct Employee {
    short emp_id;
    char emp_name[20];
    float emp_salary;
};

This defines a structure called Employee which contains three variables of different data types: a short integer for emp_id, a character array of length 20 for emp_name, and a floating-point number for emp_salary.

You can then create variables of type Employee by using the structure definition, like this:

struct Employee emp1;

This creates a variable emp1 of type Employee which contains the three variables defined in the structure.

If you're looking to define a structure within another structure, you can do so by simply including the structure definition as a member of another structure. For example:

struct Department {
    short dept_id;
    char dept_name[20];
    struct Employee emp[10];
};

This defines a structure called Department which contains two variables of different data types (dept_id and dept_name) and an array of 10 variables of type Employee. This means that each Department variable can contain up to 10 Employee variables.

I hope this helps! Let me know if you have any further questions.

 Size of structure:-

The size of a structure in C language that contains a short data type will depend on the size of a short on your particular system.

In most systems, a short is 2 bytes in size. However, it's always good practice to use the sizeof operator to determine the size of a structure on your system, as it may vary depending on the implementation and the compiler you're using.

For example, consider the following structure definition:

struct Employee {
    short emp_id;
    char emp_name[20];
    float emp_salary;
};

The size of this structure will depend on the size of a short integer on your system. If a short integer is 2 bytes on your system, then the size of this structure will be 26 bytes (2 bytes for emp_id, 20 bytes for emp_name, and 4 bytes for emp_salary). You can use the sizeof operator to determine the size of this structure on your system as follows:

printf("Size of Employee structure: %d bytes", sizeof(struct Employee));

This will print out the size of the Employee structure in bytes. Note that the actual size of a structure may be larger than you expect due to padding bytes that are inserted by the compiler for memory alignment purposes.

 Union definition:-

In C language, a union is a user-defined data type that allows you to define a new data type which can hold different types of data at different times, but occupies the same memory space. The size of a union will be the size of its largest member.

Here's an example of a union definition that contains a short data type:

union Data {
    short num;
    char ch[2];
};

This defines a union called Data which contains two members: a short integer called num, and a character array called ch with a length of 2. The size of the union will be 2 bytes (the size of a short), as it is the largest member of the union.

You can use the union in your program to hold different types of data at different times. For example, you can assign a value to the num member of the union and then access the same value as a character array through the ch member, like this:

union Data data;
data.num = 0x6162; // Assign the value 'ab' in hexadecimal
printf("num = %d, ch[0] = %c, ch[1] = %c\n", data.num, data.ch[0], data.ch[1]);

This will output num = 24930, ch[0] = a, ch[1] = b, showing that the same data can be accessed as a short integer or as a character array through the union. Note that this is an example of type punning, which can have undefined behavior in some cases, so use unions with caution.






Post a Comment

Previous Post Next Post