Easy Tutorial
❮ Verilog Numerical Conversion All Vim Cheatsheat ❯

Struct Variables and Struct Type Definition

Classification Programming Techniques

1. Struct Type Definition

Definition Method 1:

Typedef struct LNode {
   int data;  // Data field
   struct LNode *next;  // Pointer field
} *LinkList;

Definition Method 2:

struct LNode {
   int data;  // Data field
   struct LNode *next;  // Pointer field

};
Typedef struct LNode *LinkList;

These two definition methods are equivalent, defining LinkList as a type of struct LNode, meaning LinkList is defined as a type name. This allows the use of LinkList to define and declare new variables, such as:

LinkList L;

This defines L as a pointer variable of the *struct LNode type.

2. Struct Type Variable Definition

Definition Method 1:

struct LNode {
   int data;  // Data field
   struct LNode *next;  // Pointer field

}LnodeA;

Definition Method 2:

struct LNode {
   int data;  // Data field
   struct LNode *next;  // Pointer field

};
struct LNode LnodeA;

These two definition methods are also equivalent, defining LnodeA as a variable of the struct LNode type, meaning LnodeA is a variable name of the struct LNode type.


Definition of Tags Inside Structures

1. Structure, Transparent Table, DATA ELEMENT, DOMAIN

A transparent table is a logical description of a physical table, containing many fields, and some of these fields are defined as PRIMARY KEYS. Within these fields, there are DATA ELEMENTS that describe language attributes and technical attributes. DATA ELEMENTS also contain DOMAINS, which define data types and field lengths.

A structure is generally used to define a structural variable, for temporary data storage, without PRIMARY KEYS, and includes COMPONENTS instead of FIELDS.

2. Attributes in Internal Table are divided into three types: LINE TYPE, KEY, and TABLE KIND.

LINE TYPE refers to an individual row in an INTERNAL TABLE, with each row having the same structure.

KEY is equivalent to a primary key in a database, which is useful for sorting, either UNIQUE or NON-UNIQUE.

TABLE KIND is divided into STANDARD, SORTED, and HASHED.

Definitions of the Three Types of Internal Tables

Standard Table:

DATA itab1 TYPE STANDARD TABLE OF scarr WITH NON-UNIQUE KEY carrid.

Sorted Table:

DATA itab2 TYPE SORTED TABLE OF scarr WITH NON-UNIQUE KEY carrid.

Hashed Table:

DATA itab3 TYPE HASHED TABLE OF scarr WITH UNIQUE KEY carrid.

General Definition Method (Standard Table)

Define a standard table based on a database table

DATA itab4 TYPE TABLE OF scarr.

Define a standard table based on a self-built structure variable (most commonly used)

DATA: BEGIN OF wa,
    carrid TYPE scarr-carrid,
    carrnamen TYPE scarr-carrname,
    END OF wa.
DATA itab5 LIKE TABLE OF wa.

Define a standard table based on a table type in the data dictionary

DATA itab6 TYPE ztabtype00_1.

Define an internal table based on an internal table

DATA itab7 LIKE itab6.

Define a structure based on an internal table

DATA wa1 LIKE LINE OF itab7.
  1. Methods of Defining Structures

Define program structure variables (or types) based on tables or structures in the data dictionary

types str1 type spfli.
data str2 type sflight.

Define custom program structure variables (or types) - most common

data: begin of wa,
carrid type spfli-carrid,
com(10) type c,
end of wa.

Define a structure based on an internal table

data wa like line of itab.

Note: Structures defined based on database tables must be structures.

Assignment of fields with the same name (important)

Move-corresponding A to B.

Read is to read a piece of data from an internal table

read table itab like table of wa.

To read multiple pieces of data from an internal table, use a loop

loop at itab into wa.

Interpretation in C/C++ Language

Struct Definition

A struct is a collection of data consisting of a series of elements of float wage1, wage2, wage3; } Tianyr, Liuqi;

Use of Structure Variables

A structure is a new data type, so structure variables can also be assigned and operated like other types of variables, with the difference being that structure variables use members as basic variables.

The representation of structure members is as follows:

Structure variable.member name

If the structure variable.member name is seen as a whole, this whole has the same data type as the member in the structure, so it can be used like the variables mentioned earlier.

The following example defines a structure variable, each member of which receives data from the keyboard, then sums the floating-point numbers in the structure, and displays the result of the operation. Note the access to different structure members in this example.

Example

#include <stdio.h>
#include <conio.h>

int main()
{
    struct
    {
        char name[8];
        int age;
        char sex[4];
        char depart[20];
        float wage1, wage2, wage3;
    } a;
    float wage;
    char c = 'Y';
    while (c == 'Y' || c == 'y')
    {
        printf("Name:");
        scanf("%s", a.name);
        printf("Age:");
        scanf("%d", &a.age);
        printf("Sex:");
        scanf("%s", a.sex);
        printf("Dept:");
        scanf("%s", a.depart);
        printf("Wage1:");
        scanf("%f", &a.wage1);
        printf("Wage2:");
        scanf("%f", &a.wage2);
        printf("Wage3:");
        scanf("%f", &a.wage3);
        wage = a.wage1 + a.wage2 + a.wage3;
        printf("The sum of wage is %6.2f\n", wage);
        printf("Continue?");
        c = getche();
    }
}

Structure Arrays and Structure Pointers

A structure is a new data type and can also have structure arrays and structure pointers.

1. Structure Arrays

A structure array is a collection of variables of the same structure type. If you want to define the names, genders, ages, and addresses of 40 students in a class, you can define it as a structure array. As shown below:

struct
{
    char name[8];
    char sex[4];
    int age;
    char addr[40];
} student[40];

It can also be defined as:

struct string
{
    char name[8];
    char sex[4];
    int age;
    char addr[40];
};
struct string student[40];

It should be noted that the access to the members of a structure array is based on the array element as a structure variable, and its form is:

Structure array element.member name

For example:

student[0].name
student[30].age

In fact, a structure array is equivalent to a two-dimensional structure, where the first dimension is the structure array element, each element is a structure variable, and the second dimension is the structure member.

Note: The members of a structure array can also be array variables.

For example:

struct a
{
    int m[3][5];
    float f;
    char s[20];
} y[4];

To access the variable in the structure a of the structure variable y[2], it can be written as y[2].m[1][4].

2. Structure Pointers

A structure pointer is a pointer pointing to a structure. It is defined by an * operator added before the structure variable name, for example, to define a structure pointer using the previously explained structure:

struct string
{
    char name[8];
    char sex[4];
    int age;
    char addr[40];
} *student;

The structure pointer name can also be omitted and only the structure is explained, and then the following statement is used to define the structure pointer.

struct string *student;

The access to the structure members by the structure pointer is different from the access of the structure variable to the structure members in the expression. The access of the structure pointer to the structure members is represented as:

Structure pointer name->structure member

The -> is a combination of two symbols - and >, resembling an arrow pointing to the structure member. For example, to assign values to name and age in the structure defined above, the following statements can be used:

``` strcpy(student->name, "Lu G.C");

❮ Verilog Numerical Conversion All Vim Cheatsheat ❯