programming language

What is structure in C language

In the C programming language, a structure is a user-defined data type that allows you to group together different variables of different types under a single name. It provides a way to organize related data into a single unit.

The syntax for defining a structure in C is as follows:

struct structureName {
    dataType1 member1;
    dataType2 member2;
    // ...

Here, structureName is the name given to the structure, and dataType1, dataType2, and so on represent the data types of the members within the structure.

For example, let’s define a structure called Person that stores information about a person’s name and age:

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

In the above example, the Person structure has two members: name of type character array (char[50]) and age of type integer.

Once a structure is defined, you can create variables of that structure type:

struct Person person1;

You can access the members of a structure using the dot . operator:

strcpy(, "John Doe");
person1.age = 25;

Structures allow you to group related data together and pass them as arguments to functions, store them in arrays, and use them to create more complex data structures like linked lists or trees.

It’s worth noting that in C++, the struct keyword is optional, and you can define structures in a more simplified manner.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button