C Language Basics 07

Source: Internet
Author: User

The difference between a struct and a function is that a function is made up of variables of the same data type, and there can be different combinations of data types in the structure.

struct struct Name {

Data type members; //note must be separated by semicolons ;

...

}

To create a construction body

struct Student {

int age;

Char name[45];

char gender;

Float score;

}

//Create a struct variable and assign it a value

struct student Stu ={18, "Luoshuai", ' m ', 87,5};

However: struct student stu1; stu1={18, "Luoshuai", ' m ', 87,5};

Error, if at the time of declaration, not all assigned value, can not be placed in the back to all assignments.

However, you can assign values individually

struct student stu1;

Stu1.age = 18;

Stu1.name= "Lihuahua"; Arrays of strings or arrays cannot be directly assigned to each other using functions.

strcpy (Stu1.name, "Luohuahua");

typedef renamed. Can simplify the function name of the system

typedef float FF;

FF score = 98.5;

typedef and struct combinations used, often used later

typedef struct {

int age;

Char name[30];

char gender;

float weight;

} Cat;

Nesting of structural bodies

typedef struct{

int year;

int mouth;

int day;

} Birthday;

typedef struct{

int age;

Char name[30];

  Birthday Bir;

} people;

People p1 = {"Jiesi", 29847901093,{1990,6,12}};

printf ("%d\n", p1.bir.year);

One more thing we need to note is that arrays of strings or arrays cannot be directly assigned to other variables. But the structure is possible between each other. So if you want to swap arrays, you can use structs.

typedef struct {

int age; 4 bytes

Double score; 8 bytes

char gender; 1 bytes

Char name[20]; 20 bytes

} Student;

How do we know where a struct is in the memory footprint? In fact, it is very simple, the above has been calculated for each variable memory bytes Occupy, add up is 33

Our largest member variable data type is the unit, which is obviously a double of 8 bytes, then 8 * n >=, n must go to the value 5. The final memory consumption is 5*8=40 bytes. Often encountered during the interview!

C Language Basics 07

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.