Follow Freshman Meng Xin can read the c language version (I)_ Why_does_it_work blog - CSDN blog
Then I want to talk about the data
data
During initial programming, after we write the header file, we operate the data through multiple functions and a main function or through a main function. What is the data? As mentioned in the previous article, there are basic types, construction types, pointer types and null types viod
I want to talk about pointer type data, which was not mentioned in the previous article. We know that pointer is address and pointer variable is variable. They are two different things. This time, I want to think about a problem from a new perspective. What is address and what is it doing?
In fact, everyone knows that cpu processors access memory units through addresses. When we talked about variables in the previous article, we said that variables are composed of variable names, variable values and storage units. Are memory units and addresses really the same?
Obviously, it's different. These are all due to the nonstandard oral expression, which has brought me a lot of trouble (I don't know if you have such a problem). The cpu points to the memory unit through the address because the address, as a data, has directivity. The address refers to the needle, the pointer is the address, and the address points to the memory unit because of the directivity, which is to point to the data, So the pointer has the function of pointing to data. What about the pointer pointing to the variable? The definition of the pointer variable is that the pointer variable points to the variable value, and the variable stores the address pointing to the variable value. In fact, it is a variable. The variable here refers to the basic data type, integer, floating point, etc. the pointer is very powerful, and it can also point to a function (the comments in the last issue were explained by the boss of the district). You can also use pointer variables in functions. After listening to the above description, I hope you can gain something. You can have a new understanding of pointers, pointer functions and pointer arrays from the perspective of data
Structure (struct)
In the learning process, we put a very long string of very many numbers. When it is difficult to handle, we think of arrays. What if many variables are difficult to handle?
Some related variables or less related variables are combined to describe as a whole object
This is the struct ure. You can simply understand it as a data set.
definition:
sturct Structure name {List of members; };List of variable names; //Be sure to add a semicolon
When you know how to define, you must not forget the semicolon. After defining the structure, you must define the structure variable
The picture is more intuitive. In short, it is to integrate different data types into an organic whole for future use
Operate it!
Member list:
struct student { int num; char name[20]; char sex; float score; };
It can be concluded from the above code that we can not simply understand the set of variables. It is a set of several ordered variables with fixed number and different types. As can be seen from the structure definition, the structure allows different types of data structures to form a whole.
We can also simplify it by defining
#define ABC struct student ABC { int num; char name[20]; char sex; float score; };
I've been waiting for a long time... I can't slow down after trying for a long time 🤣
Initialization of structure
In the use of structure, it is necessary to define structure variables to realize the subsequent call operation, which can be initialized like other variables
#include <stdio.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book = {"Can understand the version", "Freshman Mengxin", "programing language", 210448596}; int main() { printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id); return 0; }
Output results:
title: you can understand the version
author: Freshman Meng Xin
subject: programming language
book_id: 210448596(qq)
When we use variables inside this structure, we can use two symbols "." and "-" > "
In my opinion, just remember one sentence
When the structure is a pointer, you use - > to refer to the member of the structure, but if it is not a pointer
Having finished using the structure, consolidate your words with a question
Write a program, input the student number of n (n < 10) students (the student number is a 4-digit integer, starting from 1000) from the keyboard, store the results in the structure array, find and output the information of the students with the highest scores.
#include <stdio.h> #define N 8 typedef struct student { int no; int score; }student; int main() { student s[1000]; int n,i; int max; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&s[i].no); scanf("%d",&s[i].score); max=s[0].score; if(max<s[i].score) max=s[i].score; } for(i=0;i<n;i++) { if(s[i].score==max) printf("%d ",s[i].no); } printf("%d",max); return 0; }
In terms of refinement, a structure can be divided into structure variables, structure arrays, and structure pointers
In fact, there is little difference between structure array and structure variable, but array is often used to represent a group of data structures, such as a student's file or a workshop employee's salary table. The definition is similar. You only need to specify that it is an array
struct student { int id; int name[20]; char sex; float score; }stu[5];
It defines a structure array, stu[5]; there are five elements in total, and each array element has the structure form of struct student
Structure pointer
- When a pointer variable is used to point to a structure variable, it is called a structure pointer variable (according to the understanding of the above data point of view, the pointer is basically the concept)
- Format: struct structure name * structure pointer variable name
-
// Define a structure type struct Student { char *name; int age; }; // Define a structure variable struct Student stu = {"lnj", 18}; // Defines a pointer variable to the structure struct Student *p; // Point to structure variable stu p = &stu; /* At this time, you can access the members of the structure in three ways */ // Method 1: structure variable name. Member name printf("name=%s, age = %d \n", stu.name, stu.age); // Method 2: (* pointer variable name). Member name printf("name=%s, age = %d \n", (*p).name, (*p).age); // Method 3: pointer variable name - > member name printf("name=%s, age = %d \n", p->name, p->age); return 0; }
When the structure is a pointer, you use - > to refer to the member of the structure, but if it is not a pointer
-
There are two ways to access structure members through structure pointers:
- (* structure pointer variable). Member name
- Structure pointer variable - > member name (familiar)
-
- (pstu) must have parentheses on both sides because the member character '.' takes precedence over ''.
- If you write pstu.num without parentheses, it is equivalent to (pstu.num). In this way, the meaning is completely wrong
- So I still think that sentence is not easy to make mistakes
Structure nesting
Simply put, a structure is placed inside a structure
for example
struct Date{ int month; int day; int year; } struct stu{ int num; char *name; char sex; struct Date birthday; Float score; }
The effect is
Another is the nesting of structure variables. To be exact, a structure can only nest other structures and the address of this structure
In the following, there is a little content linked list. My previous (first) content scope is still too large. Programming is a process of slowly accumulating, and these knowledge are slowly mined. I haven't mentioned many details. For example, in the process of variable assignment, variables are used as intermediates for substitution, such as printf in input and output, Some other functions of scanf, such as those spaces, the performance of carriage return in the output results, etc. I am a freshman who has studied c language for about half a month I believe that many people will encounter some setbacks and bottlenecks at this time. I hope to slowly sort out my disordered knowledge fragments and realize the realization of knowledge and flexible application by writing some learning feelings. I will start learning c + + and python next week. I hope we can work together to share some of your learning experience and progress together. For a long time, I will make progress bit by bit and make up for the shortcomings of the article. I wish you to leave your problems and the shortcomings of my article. I will continue to write when I have time. Thank you!