Monday, April 28, 2025

C Tutorial - 20 - Structures

What are Structures in C and why do we need them

A structure in C is a user-defined data type that groups together related data items of different data types. The structure provides a way to represent complex data structures in a more organized and efficient manner. It is a composite data type that combines primitive data types such as integers, floats, and characters.

Structures are used to represent real-world objects and entities in the programming world. For example, if we want to represent a student record that includes data such as name, roll number, age, and marks, we can create a structure that contains fields for each of these data items. This structure can then be used to store and manipulate student records in our program.

Structures are useful in programming for several reasons:

  1. Group related data: Structures provide a way to group related data items together, making it easier to organize and manipulate complex data structures.

  2. Efficient memory management: By grouping related data items together in a structure, we can save memory and reduce the amount of memory needed to store and manipulate the data.

  3. Improved code readability: Structures make the code more readable and easier to understand by providing a logical and organized way to represent complex data structures.

  4. Better data organization: Structures can be used to organize data in a program, making it easier to access and manipulate data items.

  5. Enables user-defined data types: Structures enable programmers to define their own data types, allowing for more flexibility in the programming language.

Overall, structures in C provide a powerful and flexible way to organize, manage, and manipulate data in a program, making it easier to represent complex data structures and improve the efficiency of the program.

 

C Structure Example

#include <stdio.h>
#include <string.h>

struct Books
{
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main()
{

    struct Books Book_1;

    strcpy(Book_1.Title, "Relativity T");
    strcpy(Book_1.Author, "Albert E");
    strcpy(Book_1.Topic, "Relativity");
    Book_1.id_book = 4323;

    printf("Book_1 - Title: %s \t\n", Book_1.Title);
    printf("Book_1 - Author: %s \t\n", Book_1.Author);
    printf("Book_1 - Topic: %s \t\n", Book_1.Topic);
    printf("Book_1 - ID: %d \t\n", Book_1.id_book);

    printf("-------------------------- \n");

    return 0;
}

Result:

Book_1 - Title: Relativity T
Book_1 - Author: Albert E
Book_1 - Topic: Relativity
Book_1 - ID: 4323
--------------------------

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.

This is an example of a program that uses a structure in C to store information about a book. The structure is defined using the keyword struct, followed by the structure name, Books, and the fields of the structure, which include the book's title, author, topic, and ID.

In the main function, a variable of type Books named Book_1 is declared, and the information about the book is stored in this variable using the strcpy() function to copy strings to the fields of the structure.

Finally, the information about the book is printed to the console using printf(). Each field of the structure is accessed using the . operator to access the field of the Book_1 variable.

This program demonstrates how structures can be used to store and manipulate complex data structures in a C program. The Books structure allows us to group related data items together in a logical and organized way, making it easier to manage and access the data.

 

Why we need dot (.) while working with Structures in C

In C, the . operator is used to access the individual fields of a structure variable. This is necessary because a structure is essentially a collection of related data items that can have different data types, and the . operator allows us to access a specific data item within the structure.

When we declare a structure variable, we are essentially creating a new type that consists of the fields defined in the structure. To access a specific field within the structure, we use the . operator to indicate which field we want to access.

For example, suppose we have a structure named Person that contains fields for a person's name, age, and address. To access the person's name field, we would use the . operator as follows:

struct Person p;
p.name = "John";

In this example, we first declare a Person structure variable named p, and then use the . operator to set the name field of the structure to "John". Without the . operator, there would be no way to access the individual fields of the structure, and we would not be able to manipulate the data stored within the structure.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...