Freshman Meng Xin can read the C language version (2)

Why_ does_ it_ work 2021-11-25 15:32:41

Following Written by freshman Meng Xin c The language can understand the version ( One )_Why_does_it_work The blog of -CSDN Blog

Then I want to talk about the data

Catalog

data

Structure (struct)

Initialization of structure

Structure pointer

Nested structure  


data


When we started programming , After we finish writing the header file , Operate on data through multiple functions and a main function or through a main function , What are the data ? The last article mentioned , There are basic types , Construction type , Pointer type and null type viod

I'd like to talk about what I didn't write in the last article , Pointer type data , We know that the pointer is the address , Pointer variables are variables , It's two different things , This time I want to have a new perspective through freshman sprouting , Think about a problem , What's the address , What is it doing ?

In fact, everyone knows ,cpu The processor accesses the memory unit through the address , When I talked about variables in the last article , The variable is defined by the variable name , Variable values and storage units constitute , Is the memory unit and address really the same ?

It's obviously different , These are all due to the nonstandard oral expression, which has brought me a lot of trouble ( I wonder if you have such a problem )cpu Point to the memory unit by address , Because the address is used as a data , It has its own directivity , The address is a pointer , The pointer is the address , The address points to the memory unit due to directivity , Is to point to data , So the pointer has the function of pointing to data , The pointer points to the variable , The definition of pointer variable is that the pointer variable points to the variable value , What a variable stores is the address that points to the value of the variable , In fact, it is a variable , Variables here refer to basic data types , plastic , Floating point and so on , The pointer is very powerful. It can also point to a function ( In the last comment, the district boss explained ), You can also use pointer variables in functions , After listening to the above , I hope you can get something , There is a new understanding of pointers and 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 deal with, we think of arrays , What if many variables are difficult to handle ?

Combine some related variables or less related variables , Describe as a whole object

this Is the structure (struct) You can simply understand it as a data set .

Definition :

sturct Structure name
{ Member list ;
}; Variable name list ;
// Be sure to add a semicolon 

  I know how to define , Make sure you don't forget the semicolon , After defining the structure , You need to define structure variables

  The picture is more intuitive , Simply put, it is to integrate different data types into an organic whole , For future use

Operate the !

Member list :

struct student
{
int num;
char name[20];
char sex;
float score;
};

From the above code, we can get , We can't simply understand the set of variables , It's a fixed number , A collection of ordered variables of different types , As you can see from the structure definition , Structure allows different types of data structures to form a whole .

We can also pass define Simplify

#define ABC struct student
ABC
{
int num;
char name[20];
char sex;
float score;
};

Waiting so long ..... After trying for a long time, I can't slow down


Initialization of structure

The structure is in use , Structure variables need to be defined to implement the subsequent call operation, which becomes simple , Like other variables, it can be initialized

#include <stdio.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book = {" Can understand the version ", " A freshman ", " 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 : Can understand the version
author: A freshman
subject: programing 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, it is used to refer to the members of the structure -> And if it's not a pointer, use .

Having finished using the structure , Consolidate with a question

Programming , Input from keyboard n (n<10) Student number of students ( The student number is 4 An integer , from 1000 Start )、 Score and store it 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;
}

If you say : Structure can be divided into structure variables , Array of structs , 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 , For example, a student's file or a workshop employee's payroll, etc , The definition is similar to , It only needs to be an array

struct student
{
int id;
int name[20];
char sex;
float score;
}stu[5];

Defines an array of structs ,stu[5]; There are five elements, and each array element has struct student The structural form of

Structure pointer

  • A pointer variable when used to point to a structure variable , Call it a structure pointer variable ( According to the above data point of view, the pointer is basically a 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};
    // Define a pointer variable to the structure
    struct Student *p;
    // Point to the structure variable stu
    p = &stu;
    /*
    You can use 3 There are two ways to access members of a structure
    */
    // The way 1: Structure variable name . Member name
    printf("name=%s, age = %d \n", stu.name, stu.age);
    // The way 2:(* Pointer variable name ). Member name
    printf("name=%s, age = %d \n", (*p).name, (*p).age);
    // The way 3: Pointer variable name -> Member name
    printf("name=%s, age = %d \n", p->name, p->age);
    return 0;
    }
    

    When the structure is a pointer, it is used to refer to the members of the structure -> And if it's not a pointer, use .

  • Access structure members through structure pointers , There are two ways

    • (* Structure pointer variable ). Member name
    • Structure pointer variable -> Member name ( Well done )
    • (pstu) The brackets on both sides are indispensable , Because members “.” Has a higher priority than “”.
    • Write without brackets pstu.num Equivalent to (pstu.num), such , The meaning is totally wrong
    • So I still think that sentence is not easy to make mistakes

Nested structure  

In short, 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


There is a little bit of content linked list in the follow-up , Before me ( Chapter one ) The scope of the content is still too large , Programming is a process of slow accumulation , This knowledge is a process of being excavated slowly , Many details I didn't mention , For example, in the process of variable assignment, variables are used as intermediates for substitution , For example, in the input and output, except printf,scanf Other functions , Like those spaces , The performance of carriage return in the output result, etc , I'm a student c Language freshmen who are about half a month old   I believe that many people will encounter some setbacks and bottlenecks at this time , I hope to sort out my disordered knowledge fragments slowly by writing some learning feelings, so as to realize the realization of knowledge and flexible application , I'm going to start learning next week c++ and python May we work together , Share with me some of your learning experiences and make progress together . For a long time, I will make progress bit by bit , Make up the deficiency of the article , May you leave your questions and the shortcomings of my article , I will continue to write when I have time , Thank you. !

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25