C structures

In this lesson, we discuss C structures. The structure gives you much power when you want to code in C. Using structures, you can work with the data much more flexible.

What are C structures?

As we’ve learned so far, arrays can be used to collect a group of the same data type; But it is impossible to define an array that contains, for example, five elements of integer type and five elements of decimal type. On the other hand, in various programming applications, it is necessary to define different elements together and assign them a name so that their collection can be processed at once (such as rewriting them at once, sending them all to a function, or preparing and returning all of them as the result of a function).

Suppose we want to define the data related to a student, such as a student number, first name, last name, gender, and grade, which have different types under one name. Or, as another example, we want to store the information related to a company’s employees, which includes the employee’s name (character type), employee number (integer type), salary (integer type), etc., under one name. In this case, Normal variables and arrays do not meet our needs.
We want to know how to define a type that contains different but related data types.

Structure definition in C language

We can group variables of different types by a structure. Therefore, structure in C language is a name for a set of variables that not necessarily are the same type. That is, the structure can have different types of data, including float, int, char, etc., under one name, and the user can access them at any time. We can see the structure definition format below:

struct [structure tag]{
    member definition  
    member definition  
    ...  
    member definition  
}[one or more structure variables];
  1. [ ] is not used in writing the program, and it is only used in this step to show the general format.
  2. The name of the structure or (structure tag) follows the naming rule for variables that we mentioned in the variables lesson.
  3. Structure members are variables whose name and type must be known.
  4. Structure variables are the variables that are supposed to have members, like structure members.
  5. To use the elements of the introduced structure, variables of the structure type must be introduced after that.

There are two ways to introduce structure type variables. Below you can see the template of the first method  :

The first method

struct <structure name>{
   //structure memebers
}<variable names>;

To understand the first method, consider the following example:

struct student_record{
    long  student_number;  
    char  first_name[21];  
    char  last_name[31];  
    char  gender_code;  
    float average;  
    int   passed_units;  
}student1;
  1. On line one, a structure named student record is defined.
  2. Note that the structure’s name is not a variable but only a name for that structure. For example, a structure with the name “student_profile” could be defined.
  3. Now, each student uses this structure separately to store their information. Therefore, a separate variable of this structure type should be defined for each student.
  4. On lines 2 to 7, the types of variables to store student profile information are defined.
  5. On line 8, a variable named student1 of the student_record type is defined. So, only this one memory variable is occupied in the memory. student1 is the name of a variable that has six members.
  6. If we want to define more variables of this structure, just separate them with a comma after student1.
  7. If a variable name is not written after the end of the structure definition (after { ), only one structure is defined, and since no variable is defined, no memory is occupied.

The second method

If a structure type variable is not written; In the rest of the program, you can use the word “struct” and the name of the structure to define the required variables. Below you can see the second method:

struct <structure name> <variable names>;

Pay attention to the example below:

struct student_record S1, S2;
  1. Variables called S1 and S2 of the “student_record” type have been introduced. Each of these variables contains the entire introduced structure. 
  2. Note that in the second method, the structure must be defined in advance so that variables of its type can be defined here.
  3. We can define any number of variables separated by commas.

Structure Initialization

Structure initialization (similar to array initialization) is allowed. For example, first, we define a structure to store the information of a student.

struct student{
    long int id;  
    char name[20];  
    float average;  
    int age;  
};

Then we define a variable of the type student structure named “s” and initialize it as follows:

struct student s = {83201012, "Shawn", 3.75, 19};
  1. A structure named student includes 4 internal members for student information.
  2. A variable called s is defined as the student structure type.
  3. The variable s contains all the elements defined in the desired structure, and the C compiler automatically allocates the necessary memory to this structure variable.

In initialization, a set of values ​​can be assigned to a structure, but it is not possible to do this in the middle of code as an executive command.

Access to structure elements

To assign values ​​during the program or to read the values ​​in the structure, we need to access its elements. You can see the format of access to structure elements below :

<structure variable>.<structure memeber name>
  1. The name of a structure variable and the name of a structure member are linked to each other with “.“.
  2. This operator, which has the highest priority, is executed from left to right.
  3. It should be noted that by convention, there is no blank space on both sides of the dot operator
  4. If the internal elements of the structure is of array type, it is necessary to mention the array index to access that element.

Now we want to assign values ​​to the elements of the structure variable “s” in the middle of the program.:

s.id = 990133710;
strcpy(s.name, "Ehsan");
s.average = 3.75;
s.age = 18;
  1.  The structure variable name is written first and then the dot. After that, the name of the desired element of the structure is written, and initialization is done.
  2. On line 2, we cannot set the name string, which is a member of the structure, as “s.name = Ehsan.” Because this is only allowed in the initial definition.
  3. Therefore, we must include the string.h library in the program and use the strcpy function.

Structure assignment in C language

The only assignment operation that is defined for structure in C language is the assignment of a structure to another structure with the same body(both structures are defined through a struct). In this case, the contents of each element from the source structure to the corresponding element of the destination structure are moved. A source structure can be a variable in a program or the output of a function.

An array of structures

In C, an array name can be placed in front of a structure name to declare an array of structures. One of the most common uses of structures is to be an array element. To define an array of structures, first, define the structure type, then define an array of structure type. To understand, consider the following example:

struct student{
    char name[21];  
    int stno;  
    float ave;  
};

struct student st[100];
  1. The 100-element array st is defined so that each element is of the student structure type.
  2. In each array element, the information of a student can be stored separately.

Passing structures to functions

Calling the function means sending arguments to the function and receiving the output. Calling is done in two ways. Calling the function by value (call by value) and calling the function by reference (call by reference).

In calling the function, a structure-type variable can be sent as an input argument to the function. Passing a structure to a function sends a full copy of the structure to the function. This will consume more memory and slow down the program. A pointer can be used to solve this problem. Read more bout function in its lesson.

Passing the structure’s address to the function using pointers is more efficient. The function can then use the address to access the structure’s members directly. When the structure is transferred to the function by calling the reference method, the speed of operations on them increases. Therefore, while calling the functions, it is better to transfer their address instead of the structure. To transfer the address, we must use c pointers and structures. This feature makes C very fast, which we discussed in the feature of the C language lesson.

Was this helpful?
[0]