C language: rookie tutorial - structure

Keywords: Programming C

Rookie tutorial - C structure

The C array allows you to define variables that can store items of the same type,
Structure is another user-defined available data type in C programming,
It allows you to store different types of data items.

Structure is used to represent a record,
If you want to track the dynamics of books in a library, you may need to track the following properties of each book:
Title
Author
Subject
Book ID

Define structure

In order to define a structure, you must use a struct statement.
The struct statement defines a new data type with multiple members. The format of the struct statement is as follows:

struct tag { 
    member-list
    member-list 
    member-list  
    ...
} variable-list ;

/*
tag Is the structure label.
member-list Is a standard variable definition, such as int i; or float f, or other valid variable definitions.
variable-list Structure variable, defined at the end of the structure, before the last semicolon, you can specify one or more structure variables.
*/

Here is how to declare the Book structure:

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

In general, there should be at least two tag s, member list and variable list. Here is an example:

//This declaration declares a struct with three members: an integral A, a character b and a double c
//At the same time, structure variable s1 is declared
//The structure is not labeled
struct 
{
    int a;
    char b;
    double c;
} s1;
 
//This declaration declares a struct with three members: an integral A, a character b and a double c
//The label of the structure is named SIMPLE, and no variables are declared
struct SIMPLE
{
    int a;
    char b;
    double c;
};
//In addition, variables t1, t2, t3 are declared with the structure of SIMPLE tag
struct SIMPLE t1, t2[20], *t3;
 
//You can also create new types with typedef
typedef struct
{
    int a;
    char b;
    double c; 
} Simple2;
//Now you can declare a new struct variable with Simple2 as the type
Simple2 u1, u2[20], *u3;

In the above declaration, the first and second declarations are treated as two completely different types by the compiler, even if their member list is the same, it is illegal to make T3 = & S1.

The members of a structure can contain other structures or pointers to its own structure types, which are usually used to implement more advanced data structures such as linked lists and trees.

//The declaration of this struct contains other structs
struct COMPLEX
{
    char string[100];
    struct SIMPLE a;
};
 
//The declaration of this structure contains a pointer to its own type
struct NODE
{
    char string[100];
    struct NODE *next_node;
};

If two structures contain each other, you need to make an incomplete declaration for one of them, as follows:

struct B;    //Incomplete declaration of structure B
 
//Structure A contains A pointer to structure B
struct A
{
    struct B *partner;
    //other members;
};
 
//Structure B contains A pointer to structure A. after A is declared, B also declares it
struct B
{
    struct A *partner;
    //other members;
};

Initialization of structure variables

As with other types of variables, you can specify an initial value when defining a structural variable.

#include <stdio.h>
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book = {"C language", "RUNOOB", "programing language", 123456};
 
int main()
{
    printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id);
}

/*
title : C language
author: RUNOOB
subject: programing language
book_id: 123456
*/

Access structure members

To access members of a structure, we use the member access operator (.).
The member access operator is a period between the name of the structure variable and the structure member we want to access.
You can use the struct keyword to define variables of the structure type.
The following example demonstrates the use of structure:

#include <stdio.h>
#include <string.h>
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        /* Declare Book1, type Books */
   struct Books Book2;        /* Declare Book2 of type Books */
 
   /* Book1 Detail */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;
 
   /* Book2 Detail */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* Output Book1 information */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);
 
   /* Output Book2 information */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);
 
   return 0;
}

/*
When the above code is compiled and executed, it produces the following results:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
*/

Structure as function parameter

You can take a structure as a function parameter, which is passed in a similar way to other types of variables or pointers.
You can use the above example to access structure variables:

#include <stdio.h>
#include <string.h>
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
/* Function declaration */
void printBook( struct Books book );

int main( )
{
   struct Books Book1;        /* Declare Book1, type Books */
   struct Books Book2;        /* Declare Book2 of type Books */
 
   /* Book1 Detail */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;
 
   /* Book2 Detail */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* Output Book1 information */
   printBook( Book1 );
 
   /* Output Book2 information */
   printBook( Book2 );
 
   return 0;
}
void printBook( struct Books book )
{
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}
/*
When the above code is compiled and executed, it produces the following results:
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
*/

Pointer to structure

You can define pointers to structures in a similar way to defining pointers to variables of other types, as follows:

struct Books *struct_pointer;

Now, you can store the address of the structure variable in the pointer variable defined above.
To find the address of a structure variable, place the & operator before the structure name, as follows:

struct_pointer = &Book1;

In order to access members of a structure using a pointer to the structure, you must use the - > operator, as follows:

struct_pointer->title;

Let's rewrite the above example with structure pointer, which will help you understand the concept of structure pointer:

#include <stdio.h>
#include <string.h>
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
/* Function declaration */
void printBook( struct Books *book );

int main( )
{
   struct Books Book1;        /* Declare Book1, type Books */
   struct Books Book2;        /* Declare Book2 of type Books */
 
   /* Book1 Detail */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;
 
   /* Book2 Detail */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* Output Book1 information by sending the address of Book1 */
   printBook( &Book1 );
 
   /* Output Book2 information by sending the address of Book2 */
   printBook( &Book2 );
 
   return 0;
}
void printBook( struct Books *book )
{
   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}
/*
When the above code is compiled and executed, it produces the following results:
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
*/

Posted by jasonyoung on Tue, 09 Jun 2020 22:36:41 -0700