- C language
- Basic grammar
- here
Structure-Composite Data Type
Structures are composite data types that can store multiple types of data.
Consider the data in a book. Suppose a book has the information "ID", "name", and "price". This information is represented by a structure.
#include <stdint.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };;
The data that is the element of the structure is called a member variable. "Id", "name" and "price" are member variables.
"Id" and "price" are " int32_t type", and "name" is " const char * Type ".
To access a member variable, use "structure variable name.member variable name".
Use of structures
Let's use a structure. Structures are available as types. The structure is declared, the value is assigned to the member variable, and the content is output.
#include <stdint.h> #include <stdio.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Declaration of structure variables struct myapp_book book; // Assign a value to a member variable book.id = 1; book.name = "C99 Book"; book.price = 2000; printf("id:%d, name:%s, price:%d\n", book.id, book.name, book.price); }
Initialization of structure variables
Structure variables can be initialized first.
Specify the values in the order of the member variables of the structure
It is a method to specify the values in the order of the member variables of the structure.
#include <stdint.h> #include <stdio.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Declare and initialize structure variables (specify values in the order of member variables) struct myapp_book book = {1, "C99 Book", 2000}; printf("id:%d, name:%s, price:%d\n", book.id, book.name, book.price); }
Initialize structure member variables with 0
I will introduce a simple syntax for initializing a structure with 0. This is an initialization syntax that takes advantage of the C language specification that 0 (all bits are 0) is used as the initial value if no initial value is specified.
#include <stdint.h> #include <stdio.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Declaration of structure variables and 0 initialization struct myapp_book book = {0}; printf("id:%d, name:%s, price:%d\n", book.id, book.name, book.price); }
Output result.
id: 0, name: (null), price: 0
Specify the member variable name of the structure and specify the value
You can specify a value by specifying the member variable name of the structure. This is the syntax added in C99.
#include <stdint.h> #include <stdio.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Declare and initialize structure variables (specify member variable names and specify values) struct myapp_book book = {.id = 1, .name = "C99 Book", .price = 2000}; printf("id:%d, name:%s, price:%d\n", book.id, book.name, book.price); }
Structure dynamic memory allocation
Let's allocate dynamic memory for the structure in the heap area. If you want to allocate memory dynamically, use the pointer type of the structure. In this example it is "struct myapp_book *".
This is a sample that uses malloc function and calloc function. It is recommended because calloc initializes to 0, but remember that the malloc function is also a standard method.
The malloc function and calloc function can be used by reading "stdlib.h".
The argument of the malloc function specifies the size of the structure. You can get the size of the structure with sizeof operator.
The first argument of the malloc function specifies the size of the structure. You can get the size of the structure with sizeof operator. The second argument is the number of elements, but here it is "1" because there is only one.
When accessing a member variable from the pointer type of a structure, use "->" instead of ".".
After securing and using it, release it with free.
#include <stdint.h> #include <stdio.h> #include <stdlib.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Structure dynamic memory allocation (malloc) struct myapp_book * book0 = malloc (sizeof (struct myapp_book)); book0->id= 1; book0->name= "C99 Book"; book0->price= 2000; // Dynamic memory allocation of structure (calloc .0 safe because it initializes) struct myapp_book * book1 = calloc (sizeof (struct myapp_book), 1); book1->id= 2; book1->name= "C Function Book"; book1->price= 3000; printf("[book0] id:%d, name:%s, price:%d\n", book0->id, book0->name, book0->price); printf("[book1] id:%d, name:%s, price:%d\n", book1->id, book1->name, book1->price); // Memory release free (book0); free (book1); }
Output result.
[book0] id: 1, name: C99 Book, price: 2000 [book1] id: 2, name: C Function Book, price: 3000
Structure array
Let's create an array of structures. Please refer to the C language array article for the array.
#include <stdint.h> #include <stdio.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Declaring an array of structures struct myapp_book books [2]; // Assign values to member variables of array elements books [0] .id = 1; books [0] .name = "C99 Book"; books [0] .price = 2000; books [1] .id = 2; books [1] .name = "C Function Book"; books [1] .price = 3000; printf("books [0] id:%d, name:%s, price:%d\n", books [0] .id, books [0] .name, books [0] .price); printf("books [1] id:%d, name:%s, price:%d\n", books [1] .id, books [1] .name, books [1] .price); }
outputresult.
books [0] id: 1, name: C99 Book, price: 2000 books [1] id: 2, name: C Function Book, price: 3000
Dynamic allocation of structure array memory
Let's dynamically allocate memory for an array of structures. I will show you how to secure with calloc. calloc is safe and recommended as it will initialize the element to 0.
#include <stdint.h> #include <stdio.h> #include <stdlib.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Dynamic memory allocation for the structure array (calloc .0 is safe because it initializes) struct myapp_book * books = calloc (sizeof (struct myapp_book), 2); books [0] .id = 1; books [0] .name = "C99 Book"; books [0] .price = 2000; books [1] .id = 2; books [1] .name = "C Function Book"; books [1] .price = 3000; printf("books [0] id:%d, name:%s, price:%d\n", books [0] .id, books [0] .name, books [0] .price); printf("books [1] id:%d, name:%s, price:%d\n", books [1] .id, books [1] .name, books [1] .price); // Memory release free (books); }
Output result.
books [0] id: 1, name: C99 Book, price: 2000 books [1] id: 2, name: C Function Book, price: 3000
For malloc, change the calloc part as follows. In this case, note that the elements of the array (each member variable of the structure) are not zero-initialized.
struct myapp_book * books = malloc (sizeof (struct myapp_book) * 2);
Where do you write the definition of the structure?
The structure definition is written in the header file. This is a sample code for split compilation when the structure definition is split into headers.
myapp_book.h
#ifndef MYAPP_BOOK_H #define MYAPP_BOOK_H #include <stdint.h> // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; #endif
myapp.c
#include <stdio.h> #include "myapp_book.h" int main (void) { // Declaration of structure variables struct myapp_book book; // Assign a value to a member variable book.id = 1; book.name = "C99 Book"; book.price = 2000; printf("id:%d, name:%s, price:%d\n", book.id, book.name, book.price); }
Compile and run
gcc -o myapp myapp.c && ./myapp
Do I have to write "struct ○○" at any time?
You can also use the typedef statement to give an alias to the type name "struct 〇〇".
#include <stdint.h> #include <stdio.h> // Give "struct myapp_book" another name "MYAPP_BOOK" typedef struct myapp_book MYAPP_BOOK; // Definition of a structure that represents book information struct myapp_book { int32_t id; const char * name; int32_t price; };; int main (void) { // Declare and initialize structure variables (specify member variable names and specify values) MYAPP_BOOK book = {.id = 1, .name = "C99 Book", .price = 2000}; printf("id:%d, name:%s, price:%d\n", book.id, book.name, book.price); }
Please refer to the following contents.
What is the memory area of the structure?
If declared as a structure variable, the structure will be allocated on the call stack.
On the other hand, if memory is dynamically allocated using malloc or calloc, it will be allocated in the heap area.