SoFunction
Updated on 2025-03-02

Introduction to structures in C language

Arrays in C language allow defining variables of type to accommodate multiple data items of the same type, but in C programming, it allows defining different types of data items that can be used for other user-defined data types.

The structure is used to represent a record, assuming that the library's books are to be tracked. The following attributes may be tracked about each book:

  • Title - Title
  • Author - Author
  • Subject - Subject
  • Book ID - Number

Define structure
To define a structure, the struct statement of the structure must be used. The struct statement defines a new data type and the program has more than one member. The format of the struct statement is as follows:

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

The structure tag is optional, and the definition of each member is 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, before the last semicolon, one or more structure variables can be specified, 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; 

Accessing structure members
To access any member of a struct, we use the member access operator (.) Member access operator is encoded as the struct variable name and want to access the struct component. Use the struct keyword to define variables of struct type. The following is an example to explain the usage of the structure:

#include <>
#include <>
 
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( , "C Programming");
  strcpy( , "Nuha Ali"); 
  strcpy( , "C Programming Tutorial");
  Book1.book_id = 6495407;

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

  /* print Book2 info */
  printf( "Book 2 title : %s
", );
  printf( "Book 2 author : %s
", );
  printf( "Book 2 subject : %s
", );
  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
A structure can be passed as a parameter to a function, very similar to passing any other variable or pointer. Access can be like the above example that has accessed similar structure variables:

#include <>
#include <>
 
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( , "C Programming");
  strcpy( , "Nuha Ali"); 
  strcpy( , "C Programming Tutorial");
  Book1.book_id = 6495407;

  /* book 2 specification */
  strcpy( , "Telecom Billing");
  strcpy( , "Zara Ali");
  strcpy( , "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
", );
  printf( "Book author : %s
", );
  printf( "Book subject : %s
", );
  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
Very similar to define pointer structures to define pointers to any other variables as follows:

struct Books *struct_yiibaier;

Now, the address of the structure variable can be stored in the pointer variable defined above. To find the address of a structure variable, operator & will be used before the structure's name, as follows:

struct_yiibaier = &Book1;

To access members of a structure that uses a pointer to a structure, the -> operator must be used as follows:

struct_yiibaier->title;

Let's rewrite the above example using structure pointers, hoping this will make it easier for us to understand the concept:

#include <>
#include <>
 
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( , "C Programming");
  strcpy( , "Nuha Ali"); 
  strcpy( , "C Programming Tutorial");
  Book1.book_id = 6495407;

  /* book 2 specification */
  strcpy( , "Telecom Billing");
  strcpy( , "Zara Ali");
  strcpy( , "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:

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

Bit field
The bit fields allow data to be wrapped in a structure. This is especially useful when memory or storing data is invaluable. Typical examples:

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

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

C language allows us to define: bit length variables after they are defined through structure. For example:

struct packed_struct {
 unsigned int f1:1;
 unsigned int f2:1;
 unsigned int f3:1;
 unsigned int f4:1;
 unsigned int type:4;
 unsigned int my_int:9;
} pack;

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

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 entire number length of the computer. If this is not the case, some compilers can allow, while others will overlap the memory stored in the next field.

Pointers and arrays:
This is a topic that can never be avoided, first of all, is the quote:

    struct stuff *ref = &Huqinwei;
    ref->age = 100;
    printf("age is:%d\n",);

Print visible changes
The same goes for pointers

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

Structures are not exempt from the common sense, they must have arrays:

struct test{
    int a[3];
    int b;
};
//For the case where arrays and variables exist at the same time, there is a definition method as follows:    struct test student[3] =   {{{66,77,55},0},
                    {{44,65,33},0},
                    {{46,99,77},0}};
//Specially, it can be simplified to:    struct test student[3] =    {{66,77,55,0},
                    {44,65,33,0},
                    {46,99,77,0}};

Variable length structure:
An array that can be lengthened

#include <>
#include <>
#include <>
typedef struct changeable{
    int iCnt;
    char pc[0];
}schangeable;

main(){
    printf("size of struct changeable : %d\n",sizeof(schangeable));

    schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeable) + 10*sizeof(char));
    printf("size of pchangeable : %d\n",sizeof(pchangeable));

    schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeable) + 20*sizeof(char));
    pchangeable2->iCnt = 20;
    printf("pchangeable2->iCnt : %d\n",pchangeable2->iCnt);
    strncpy(pchangeable2->pc,"hello world",11);
    printf("%s\n",pchangeable2->pc);
    printf("size of pchangeable2 : %d\n",sizeof(pchangeable2));
}

Running results

size of struct changeable : 4
size of pchangeable : 4
pchangeable2->iCnt : 20
hello world
size of pchangeable2 : 4

The length of the structure itself is an int length (this int value is usually just to represent the length of the subsequent array), and the length of the subsequent array is not counted, but the array can be used directly.
(Say there is a pointer behind? The pointer also takes up the length! This one does not take up! The principle is very simple. This thing is completely the tail behind the array. malloc opens up a continuous space. In fact, this should not be considered a mechanism, it should feel like a skill)

Structure nesting:
There is nothing unexpected about structure nesting, just follow certain rules:

//For "one-time trading", you are only interested in the final structure variables, among which A and B can also be deleted, but it is best to bring it with you.struct A{ 
    struct B{
       int c;
    }
    b;
}
a;
//Use the following method to access: = 10; 

In particular, you can use it while defining structure B:

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

    struct B sb;

}a;

How to use and test:

     = 11;
    printf("%d\n",);
     = 22;
    printf("%d\n",);

The results are correct.