Basic knowledge of structure in C language

Source: Internet
Author: User

1 declaring the structure body

declares struct-body keyword struct struct body name {type modifier member name};
Declaring a student's structure

The first of these methods

struct student{
Char name[20]; Additional construction types can be used in struct members.
int number; Used between each member;
int age;
Float score;
};

typedef struct student student; The alias of the existing type modifier to Student

keyword typedef used to change aliases
The second method of
typedef struct teacher{
Char name[20];
char gender;
int age;
Char major[20];

}teacher;

The second most common

struct declarations are written on top of the main function or written in the. h file

2 use of structs (in the main function)

struct Teacher tea1 = {"Shenqingchang", ' m ', +, ' C '};
Struct member variable cannot output member information all with variable name
printf ("%s", tea1); incorrect output

Correct output

printf ("%s", tear1.name);

Modify member Names
int a = 0;
A = 10;
Change the first teacher's age to 15
Tear1.age = 15;
printf ("%d", tear1.age);
Modify the first teacher's name as "Dingchuyuan"
Tear1.name = "Dingchuyuan"; Wrong way to modify
strcpy (Tear1.name, "Dingchuyuan");
printf ("%s\n", tear1.name);
When modifying or exporting a struct, the member variable requires the struct name plus "." Add variable names to use

Example tear1.name = "Zhangsan" can be modified by writing

3 Structure Body Storage footprint
1 struct member variable, storage is from top to bottom, stored from first start
2 in memory, when the member variable is stored, the rule is: a multiple of the data type is stored, if it is occupied, the downward extension
3 Calculating the space of struct members, sum = The minimum multiple of the maximum number of bytes in a member variable
Application of 4 structure in function

Structs are used in functions that need to be written in the. h declaration file, and the other is not much different from the function

Example

A declares the structure body

#import <Foundation/Foundation.h>

Using declarative functions
typedef struct teacher{
Char name[20];
int age;
Float score;

}teacher;
Print all teacher Information
void Showinfoofteachers (Teacher array[],int count);

B implementation (definition) function

#import "Teacher.h"

Output All teacher information
void Showinfoofteachers (Teacher array[],int count) {
printf ("Teacher info: \ n");
for (int i = 0; i < count; i++) {
printf ("%-20s%-2d%.2f\n", Array[i].name,array[i].age,array[i].score);
}
}

The C call function is in the main function.

Teacher Tea[4] = {
{"Wangke", 25,90},
{"sunning", 40,89},
{"Xulaoshi", 50,78},
{"Wangcuiling", 35,91}

Showofteachers (tea, 4);

Basic knowledge of structure in C language

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.