C Language Programming - Advanced Data Types - Structures and Location Fields

Keywords: Programming C

Catalog

Article Directory

Previous List

<Program Compilation Process and GCC Compiler>
<C Language Programming - Basic Grammar>
<C Language Programming - Basic Data Types>
<C Language Programming - Variables and Constants>
<C Language Programming - Operators>
<C Language Programming - Logical Control Statements>
<C Language Programming - Functions>
<C Language Programming - Advanced Data Types - Pointer>
<C Language Programming - Advanced Data Types - Arrays>
<C Language Programming - Advanced Data Types - Enumeration>








structural morphology

Structures are another user-defined data type in C.Unlike enumeration values of enumeration types, structs allow storage of data items of different data types.

Structures are often used to represent a record. Assuming that a structure is used to track the dynamics of books in a library, we can customize data items in a structure like this:

  • Title
  • Author
  • Subject
  • Book ID

Define Structures

Use struct keywords to type struct data types:

struct tag { 
    member
    member
    member
    ...
} variable;
  • tag is the identity (name) of the structure.
  • Members are member s of several structures that define statements for standard variables, such as int i.
  • variable-list structure variable, one or more structure variables can be specified at once.

e.g.

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

Structures can be defined in a variety of ways and are more flexible.Typically, tag, member, variable-list are at least two parts.


struct {
    int a;
    char b;
    double c;
} s1;

struct SIMPLE
{
    int a;
    char b;
    double c;
};

struct SIMPLE t1, t2[20], *t3;

Note that in the declarations above, the first and second declarations are treated as two completely different types by the compiler, even if their member lists are the same.

You can also create new types with typedef:

typedef struct
{
    int a;
    char b;
    double c; 
} Simple2;

// Simple2 can now be used as a type to declare new structure variables
Simple2 u1, u2[20], *u3;

Members of a structure can be other structures or contain pointers to their own structure types, which are often used to implement more advanced data structures such as chains, lists, trees, and so on.

struct COMPLEX {
    char string[100];
    struct SIMPLE a;
};

struct NODE {
    char string[100];
    struct NODE *next_node;
};

If two structures are contained in each other, one of them needs to be declared, and the order of the statements should also be noted:

struct B;

struct A{
    struct B *partner;
    //other members;
};

struct B {
    struct A *partner;
    //other members;
};

Initialize structure variables

#include <stdio.h>

struct Books {
    char    title[50];
    char    author[50];
    char    subject[100];
    int     id;
} book = {"is book", "fanguiju", "C", 123};

int main() {
    printf("Book's title: %s\nauthor: %s\nsubject: %s\nid: %d\n", book.title, book.author, book.subject, book.id);
    return 0;
}

Access structure members

Use member access operators. Access members of a structure.

#include <stdio.h>
#include <string.h>

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

int main() {
    struct Books book1;
    strcpy(book1.title, "C Programming");
    strcpy(book1.author, "Nuha Ali");
    strcpy(book1.subject, "C Programming Tutorial");
    book1.id = 123;
    printf("Book's title: %s\nauthor: %s\nsubject: %s\nid: %d\n", book1.title, book1.author, book1.subject, book1.id);
    return 0;
}

Pass the structure into the function as an argument

#include <stdio.h>
#include <string.h>

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

void printBook(struct Books book) {
    printf("Book's title: %s\nauthor: %s\nsubject: %s\nid: %d\n", book.title, book.author, book.subject, book.id);
}
int main() {
    struct Books book1;
    strcpy(book1.title, "C Programming");
    strcpy(book1.author, "Nuha Ali");
    strcpy(book1.subject, "C Programming Tutorial");
    book1.id = 123;

    printBook(book1);
    return 0;
}

A pointer to a structure variable

A pointer type variable that defines a base class as a struct Books:

struct Books *struct_pointer;

Now you can store the memory address of the structure variable in the pointer variable defined above:

struct_pointer = &book1;

When accessing structure members using a pointer to the structure variable, you must use the -> operator as follows:

struct_pointer->title;

Because struct_pointer is essentially a memory address, unlike structure variables, member access operators cannot be used directly. Instead, the -> operator is used.

#include <stdio.h>
#include <string.h>

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

void printBook(struct Books *book) {
    printf("Book's title: %s\nauthor: %s\nsubject: %s\nid: %d\n", book->title, book->author, book->subject, book->id);
}
int main() {
    struct Books book1;
    strcpy(book1.title, "C Programming");
    strcpy(book1.author, "Nuha Ali");
    strcpy(book1.subject, "C Programming Tutorial");
    book1.id = 123;

    printBook(&book1);
    return 0;
}

Bit field

In some scenarios, data values that need to be stored do not need to occupy a complete byte (Byte) but only a few or one binary bit (Bit).For example: store a switch, only 0 and 1 states, use 1-bit binary.

To save storage space and make processing easy, C provides another data structure, the bit field.

The so-called "bit field" is to divide the binary bits in a byte into several different zones, specify the number of bits and the identification (domain name) of each zone, and allow the operation by domain name in the program.This allows several different variables to be represented in different binary bit fields of the same byte total.

Define Bit Fields

The definition of a bit field and the type of structure definition are essentially a special kind of structure:

struct bit field structure name {
    Type specifier [bit domain name]: bit field length (Bit)
    ...
};

e.g.

struct bs {
    int a:8;
    int b:2;
    int c:6;
} data;

The above bitfield structure variable data takes up two fields (16 bits).

  • A bit field is stored in the same byte (cell). When there is not enough space left for one byte to store another, the bit field is stored from the next cell.You can also intentionally start a field from the next unit:
struct bs {
    unsigned a:4;
    unsigned  :4;    /* Space, 0 means no use */
    unsigned b:4;    /* Deliberately store from next unit */
    unsigned c:4
}
  • Since bit fields do not allow cross-byte spanning, the length of a bit field cannot be greater than the length of one byte (8 Bit).If the maximum length is greater than the computer's integer length, some compilers may allow memory overlap for domains, while others may store parts larger than one domain in the next word.Depending on the implementation of the compiler, this is also one of the features of C.

  • A bit field can be an anonymous bit field, which is only used to fill or adjust positions.An unnamed bit field cannot be used.The airspace in the example above.

Using members of a bit field structure

#include <stdio.h>

int main() {
    struct BS {
        unsigned a:1;
        unsigned b:3;
        unsigned c:4;
    };

    struct BS bit;
    struct BS *pbit;

    bit.a = 1;    /* Assign an integer value of 1 to the bit field and the value does not exceed 1 bit of the bit field a */
    bit.b = 7;    /* Assign an integer value of 7 to the bit field and the value does not exceed 3 bits of the bit field b */
    bit.c = 15;   /* Assign an integer value of 15 to the bit field and the value does not exceed 4 bits of bit field c */

    printf("%d, %d, %d\n", bit.a, bit.b, bit.c);

    pbit = &bit;   /* Assigning the address of the bit domain structure variable to the pbit bit bit bit bit bit bit bit bit of the bit domain structure pointer variable */
    pbit->a = 0;   /* Structural Variables Access Structural Members */
    pbit->b &= 3;  /* And assignment */
    pbit->c |= 1;  /* Or assignment operation */
    printf("%d, %d, %d\n", pbit->a, pbit->b, pbit->c);

    return 0;
}

Function:

$ ./main
1, 7, 15
0, 3, 15

Posted by ChaosDream on Sat, 04 Apr 2020 03:16:22 -0700