Structure variables in C language (Structure Variable) and the usage of Struct and Typedef

Keywords: Struct, Typedef

Operator: . (member operator)

First, a preliminary understanding of the structure

Some people say: program = algorithm + data structure

One of the most important steps in programming is choosing a good way to represent data. Using simple variables or arrays is not enough in most cases. C uses structure variables to further enhance the ability to represent data.

The keyword Struct is used to create a structure declaration, which is the main method used to describe how structures are combined. It combines some of our commonly used data types together to form the structure we need

struct book{/*book is a template or markup */
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

Two, several declaration methods of structure variables

2.1 Marked structure variable declaration

struct book library; /*book is the markup of the structure we defined, library is the variable name we declared */

Actually this declaration is a simplification of the following declaration method:

struct book{
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
}library; /* variable name after definition*

2.2 Unmarked structure variable declaration (actually synthesize declaration and definition structure variable into one step)

struct{/*unmarked*/
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
} library;

2.3 Use typedef for variable declaration (for multiple use of defined structures)

A typedef is an advanced data feature that creates its own name for a type. In this respect it is similar to #define, however, the difference is that #define creates names for values ​​whereas typedef creates names for types.

Basic usage:

typedef unsigned char BYTE;
/* Then you can use BYTE to define data, for example: */
BYTE x,y[10],*z;

Scope of the definition: If the definition is inside a function, the scope is local. If it is defined outside a function, it is in the global scope

Therefore, there are also two ways to define a structure:

typedef struct complex{
	float real;
	float imag;
}COMPLEX
/* The first method is marked with a struct */
/* COMPLEX can be used instead of struct complex to represent complex numbers */

typedef struct {double x; double y;} rect;
/* The second method omits the markup of the structure */
/*用rect代替struct{double x; double y;}*/

3. The following is a piece of code in C (using a structure to define the data type of book):

#include<stdio.h>
#define MAXTITL 41
#define MAXAUTL 31
struct book{
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library;
    printf("Please enter the book title.\n");
    gets(library.title);
    printf("Now enter the author.\n");
    gets(library.author);
    printf("Now enter the value.\n");
    scanf("%f",&library.value);
    printf("%s by %s: $%.2f\n",library.title,library.author,library.value);

    return 0;
}

operation result:

When defining a structure, we can do the following two operations:

3.1 Accessing structure members

Use the member operator ".", such as library.title, library.author, library.value

3.2 Initializing the structure

For common data types, such as an array, how do we initialize it?

int shuzu [7] = {0,1,2,3,4,5,6}

Structures we define can also be initialized in this way:

struct book library = {
	"C_struct",
	"Linden",
	23
};

You can also use the specified initialization items in any order:

struct book library = {
	.title = "C_struct",
	.author = "Linden",
	.value = 23
};


PS: Because I am learning python recently, I implemented the above structure with classes in python

class Library:
	def __init__(self,title,author,value):
		self.title = title
		self.author = author
		self.value = value

def main():
	book_title = input("Please enter the book title.\n")
	book_author = input("Now enter the author.\n")
	book_value =  float(input("Now enter the value.\n"))
	book = Library(book_title,book_author,book_value)
	print("%s by %s: $%.2f"%(book.title,book.author,book.value))
	
main()

#The result of the operation is as follows
Please enter the book title.
Python_struct
Now enter the author.
Linden
Now enter the value.
23
Python_struct by Linden: $23.00
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326006198&siteId=291194637