It’s similar to Java’s entity, isn’t it?
Okay, this is the very first post of 2023. Let’s dive into the world of C language even father, shall we? Enjoy coding, enjoy learning and most importantly enjoy growing!
Structure basics
Structure is a collection of various types of variables, such as int, String, and char. If you have experience coding in Java, for example, it’s sort of C’s equivalent to Java’s entity. Hope it helps you better grasp the concept of it.
In image 01, the source code’s structure has int and char types of variables. Imagine a situation where you have a student database that can only handle id/name/age arrays, which is not logically efficient. In this hypothetical scenario, structure is much better and more efficient to handle different types of variables.
In image 01, struct student in line 12 is the structure-specific variable declaration. And when you need to add new variables such as gender or grades, all you have to do is to add them to the structure.
In image 02, the program is executed with the breakpoint at line 13, where the three variables, id, name, and age, are stored.
Array of structure
In image 03, an array of structure is declared in line 12. As you can see from line 17 to line 20, arrays are inserted into the structure: student_data.
Here’s the executed result.
Pointer of structure
Here’s an example of pointer of structure.
In image 05’s lines 5 to 10, the way it’s written is the shorter and more efficient version of the previous one (image 03) which is shown below. Image 06 is the old style.
As the variables declared in lines 12 and 13 have student_data* for their arguments, it passes pointers (addresses) to the student_data structure.
From lines 16 to 20, the structure’s initialized. In line 16, it’s declared that the structure can only accommodate up to four data.
In the previous program (image 03), it didn’t use pointer, so it was written in the form: data[i].id to access the variables.
But in this case (image 05), it’s written in the form (data->id) for accessing pointer variables.
From here, let’s take a look at how the program works by inserting the breakpoint in line 22.
WAs you can see, data is being inserted into the variables as you step over the program.
Ultimately, four students’ data is inserted into the database.
Pass by pointer/pass by data
Here, we’ll go through two different ways to pass data. Since structure handles different types of variables, data size is apt to get bigger than other types of data.
Here are two different types of data-passing methods – one is by the value itself and another by the pointer.
When passing data to dealData1(), the following two data will be passed on:
int number 2 for data.a in line 27
decimal number 2.4 for data.d in line 27
The concept is basically the same in dealData2 as well. But since it handles the data in pointer, it uses arrow operator(->).
Finally, in lines 14 and 15, variables n1 and n2 are declared and called the methods, dealData1, and dealData2, in lines 16 and 17. The result will be printed out in lines 18 and 19.
Here’s the executed result.
What needs your attention is the pointer addresses.
The n1’s address shown in line 1 is 0x9d6ff818.
The n1’s address shown in line 3 is 0x9d6ff940.
Therefore, what you can see here is that n1, passed by data, copies the data itself but not the address.
On the other hand…
The n2’s address shown in line 1 is 0x9d6ff848.
The n2’s address shown in line 5 is 0x9d6ff848.
So, now you can see the difference between the two. Since n2 handles the data by pointer, it copis the address itself.
Here are two reasons why passing by data is not recommended:
1: if the data is too big, it might destroy the stack area.
2: Since passing by data creates a lot of its copies, it consumes a significant amount of memories.