[Programming] C Language Structure and programming structure

Source: Internet
Author: User

[Programming] C Language Structure and programming structure

Struct

Struct name {} variable name;

Struct variable:

Struct person {

Char * name;

Int age;

Float score;

} Student;

Member acquisition and assignment

// Members of the acquisition and assignment

Student. name = "taoshihan ";

Student. age = 30;

Student. score = 100;

Printf ("name = % s \ n", student. name );

C language struct Array

Struct stu {

Char * name;

Int age;

Float score;

} Classes [5];

Traverse struct Arrays

Struct people {

Char * name;

Int age;

Float score;

} D [] = {

{"Taoshihan", 20,100 },

{"Lisi", 30, 90}

};

Int len = sizeof (d)/sizeof (d [0]);

Printf ("d length = % d \ n", len );

For (int I = 0; I <len; I ++ ){

Printf ("loop... % s, % d, %. 1f \ n ", d [I]. name, d [I]. age, d [I]. score );

}

C language struct and pointer

Struct name * variable name;

Struct person1 {

Char * name;

Int age;

Float score;

} A = {"taoshihan", 20,100}, * B = &;

Struct person1 * c = &;

Get struct members

Printf ("B. name = % s \ n", (* B). name );

Printf ("c. name = % s \ n", c-> name );

Complete code:

#include <stdio.h>int main(){        printf("hello world");        //Structure variables        struct person{                char *name;                int age;                float score;        } student;        //Members of the acquisition and assignment        student.name="taoshihan";        student.age=30;        student.score=100;        printf("name=%s \n",student.name);        //c struct array        struct stu{                char *name;                int age;                float score;        } classes[5];        struct stu1{                char *name;                int age;                float score;        } classes1[2]={                {"taoshihan",20,100.00},                {"lisi",20,90}        };        struct stu2{                char *name;                int age;                float score;        } classes3[]={                {"taoshihan",20,100}        };        printf("%s \n",classes1[1].name);        //Traverse the array of structures        struct people{"chaper5.c" 71L, 1199C                                                                                                            1,1           Top        //Traverse the array of structures        struct people{                char *name;                int age;                float score;        } d[]={                {"taoshihan",20,100},                {"lisi",30,90}        };        int len=sizeof(d)/sizeof(d[0]);        printf("d length=%d \n",len);        for(int i=0;i<len;i++){                printf("loop...%s,%d,%.1f \n",d[i].name,d[i].age,d[i].score);        }        //C language structure and pointer        struct person1{                char *name;                int age;                float score;        } a={"taoshihan",20,100},*b=&a;        struct person1 *c=&a;        //Get the structure member        printf("b.name=%s \n",(*b).name);        printf("c.name=%s \n",c->name);}

 

Related Article

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.