C Language Learning Notes-structure

Source: Internet
Author: User

Before learning enum-type enum, it feels a little unaccustomed, where it is customary to understand that the understanding of the structure here is more convenient, because there are some similarities between the two.

Register a student's name and score with a struct, if it is less than 60 points, shows bad

#include <stdio.h>


typedef struct{
Char name[20];
float points;
Char *appraise; Here with a character pointer, if you use a normal string, the outside cannot be assigned a value
} students;

Define a struct, typeof the data label name, and define the structure as a students in TypeOf.

You can also define this

struct students{//With this definition, the variable declaration behind is a struct students, such as the lucky in main to be written as struct students Lucky
Char name[20]; If a typedef can be used to take a more convenient name, to refer to the struct students
float points;
Char *appraise;
};


Students Give_appraise (students *s) {//If the preceding typedef is not defined, the struct is used here students
if (s->points<60)
S->appraise= "Bad"; Here is passed in the *s pointer can be used s-> this arrow notation, if it is only passing s, you have to use s.appraise, but also to return out, although not as the array can not return, but this obviously no pointer operation convenient
Else
S->appraise= "Good"; This pointer operation can also be written out (*s). Appraise must be bracketed because the priority of the. is greater than *
}


int main (void) {
Students lucky = {"Lucky", 49, ""};
Give_appraise (&lucky); Address of the passing structure body
printf ("%s", lucky.appraise);

return 0;
}

C Language Learning Notes-structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.