An introduction to structures in the C language

  • 2020-05-07 20:05:30
  • OfStack

Arrays in C allow you to define variables of type that can hold multiple data items of the same type, but structs in C programming allow you to define different types of data items that can be defined by other users.

The structure is used to represent a record, assuming that the library's books are to be tracked. You might want to track the following properties about each book:

Title - title Author Author - Subjects Subject - Book ID - number

defines the structure
defines 1 struct. The struct statement for the struct must be used. The struct statement defines a new data type, and the program has more than one member. The struct statement looks like this:


struct [structure tag]
{
  member definition;
  member definition;
  ...
  member definition;
} [one or more structure variables]; 

The structure (structure) tag is optional, and each member is defined as a normal variable definition, such as int i; Or float f; Or any other valid variable definition. At the end of the definition of a structure, one or more structure variables can be specified before the last semicolon, but it is optional. Here is how the statement (Book) is structured:


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

accesses struct members
To access any member of the struct, we use the member access operator (.) the member access operator is encoded as the struct variable name, and we want to access the struct part. Use the struct keyword to define variables for the struct type. The following examples illustrate 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 of type Book */
  struct Books Book2;    /* Declare Book2 of type Book */
 
  /* book 1 specification */
  strcpy( Book1.title, "C Programming");
  strcpy( Book1.author, "Nuha Ali"); 
  strcpy( Book1.subject, "C Programming Tutorial");
  Book1.book_id = 6495407;

  /* book 2 specification */
  strcpy( Book2.title, "Telecom Billing");
  strcpy( Book2.author, "Zara Ali");
  strcpy( Book2.subject, "Telecom Billing Tutorial");
  Book2.book_id = 6495700;
 
  /* print Book1 info */
  printf( "Book 1 title : %s
", Book1.title);
  printf( "Book 1 author : %s
", Book1.author);
  printf( "Book 1 subject : %s
", Book1.subject);
  printf( "Book 1 book_id : %d
", Book1.book_id);

  /* print Book2 info */
  printf( "Book 2 title : %s
", Book2.title);
  printf( "Book 2 author : %s
", Book2.author);
  printf( "Book 2 subject : %s
", Book2.subject);
  printf( "Book 2 book_id : %d
", Book2.book_id);

  return 0;
}

Let's compile and run the above program, which will produce 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 pass a structure as an argument to a function, much like passing any other variable or pointer. Access can be done in the same way as the example above has access to similar 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 of type Book */
  struct Books Book2;    /* Declare Book2 of type Book */
 
  /* book 1 specification */
  strcpy( Book1.title, "C Programming");
  strcpy( Book1.author, "Nuha Ali"); 
  strcpy( Book1.subject, "C Programming Tutorial");
  Book1.book_id = 6495407;

  /* book 2 specification */
  strcpy( Book2.title, "Telecom Billing");
  strcpy( Book2.author, "Zara Ali");
  strcpy( Book2.subject, "Telecom Billing Tutorial");
  Book2.book_id = 6495700;
 
  /* print Book1 info */
  printBook( Book1 );

  /* Print Book2 info */
  printBook( Book2 );

  return 0;
}
void printBook( struct Books book )
{
  printf( "Book title : %s
", book.title);
  printf( "Book author : %s
", book.author);
  printf( "Book subject : %s
", book.subject);
  printf( "Book book_id : %d
", book.book_id);
}

Let's compile and run the above program, which will produce 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 structure
defines a pointer structure that points to any other variable very similarly, as shown below:


struct Books *struct_yiibaier;

Now, you can store the pointer variable whose structure variable's address is defined above. To find the address of a structure variable, the operator & is used before the name of the structure, as follows:


struct_yiibaier = &Book1;

To access a member of a structure that USES a structure that points to a structure,   - must be used > The operator is as follows:


struct_yiibaier->title;

Let's go back to using the structure pointer in the following example, in the hope that this will make it easier to understand the concept:


#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 of type Book */
  struct Books Book2;    /* Declare Book2 of type Book */
 
  /* book 1 specification */
  strcpy( Book1.title, "C Programming");
  strcpy( Book1.author, "Nuha Ali"); 
  strcpy( Book1.subject, "C Programming Tutorial");
  Book1.book_id = 6495407;

  /* book 2 specification */
  strcpy( Book2.title, "Telecom Billing");
  strcpy( Book2.author, "Zara Ali");
  strcpy( Book2.subject, "Telecom Billing Tutorial");
  Book2.book_id = 6495700;
 
  /* print Book1 info by passing address of Book1 */
  printBook( &Book1 );

  /* print Book2 info by passing address of Book2 */
  printBook( &Book2 );

  return 0;
}
void printBook( struct Books *book )
{
  printf( "Book title : %s
", book->title);
  printf( "Book author : %s
", book->author);
  printf( "Book subject : %s
", book->subject);
  printf( "Book book_id : %d
", book->book_id);
}

Let's compile and run the above program, which will produce the following results:


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

a field
The
bit field allows data to be wrapped in 1 structure. This is especially useful when memory or stored data is very valuable. Typical examples:

Wraps several objects into a machine language. For example, a 1-bit flag can compress the length

Read external file formats - non-standard file formats can be read. For example: 9 bit integers.

C language allows us to define by structure :bit length after variable. Such as:


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

Here, packed_struct contains 6 members: 4 1-bit flags s f1.. f3, 1 4-bit type and 9 bit my_int.

The C language automatically wraps the above bit fields as compact as possible, provided that the maximum length of the field is less than or equal to the full length of the computer. If this is not the case, some compilers will allow, while others will overlap the memory stored in the next field.

pointer and array:
this is a never-ending topic, starting with the quote:


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

Print visible change
The pointer is also one


    struct stuff *ptr;
    ptr->age = 200;
    printf("age is:%d\n",Huqinwei.age);

Structure is not immune, must have an array:


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

variable length structure:
can vary in length


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

The results


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

The length of the structure itself is one int length (this int value is usually used to represent the length of the following array), which is not counted, but can be used directly.
(is it a pointer? Pointers also take up length! This is not occupied! The idea is simple, this thing is just the tail of the array, and malloc is creating a piece of contiguous space. It's not really a mechanism, it's more like a technique.)

structure nesting:
There is nothing too unexpected about
structure nesting, as long as it follows 1 definite rule:


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

In particular, the structure B can be defined on 1 side, and the 1 side can be used on:


struct A{
    struct B{
        int c;
    }b;

    struct B sb;

}a;

Usage and test:


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

The results were correct.

 


Related articles: