结构体

进程,线程结构,数据库字段都是简单的结构体排在一起,内存数据库中数据的组织形式,商业数据及数据间的逻辑关系,都可以用结构体来表示

  • 结构体先定义后调用,定义的时候不分配内存,创建时才分配内存
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach//mycoach是结构体类型,如果省略就是无名结构体
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
};
void main()
{
    struct mycoach cpc;
    sprintf(cpc.name,"陈培昌");//字符串不可以等号赋值,strcpy()---在string模块下
    printf("
%s",cpc.name);
    system("pause");
}

  •  一次性可以声明多个
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach//mycoach是结构体类型,如果省略就是无名结构体
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
};
void main()
{
    struct mycoach cpc,xxd,wr,fgf;
    sprintf(cpc.name,"陈培昌");//字符串不可以等号赋值,strcpy()---在string模块下
    printf("%s
",cpc.name);
    system("pause");
}
  • 宏取代
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach//mycoach是结构体类型,如果省略就是无名结构体
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
};
void main()
{
    jl cpc,xxd,wr,fgf;
    sprintf(cpc.name,"陈培昌");//字符串不可以等号赋值,strcpy()---在string模块下
    printf("%s
",cpc.name);
    system("pause");
}
  • 形式3
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach//mycoach是结构体类型,如果省略就是无名结构体
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
}cpc, xxd, wr, fgf;//定义时,就声明变量
void main()
{

    sprintf(cpc.name,"陈培昌");//字符串不可以等号赋值,strcpy()---在string模块下
    printf("%s
",cpc.name);
    system("pause");
}
  • 结构体类型和结构体变量的区别
结构体类型不分配内存,结构体变量分配,结构体类型不可以访问,赋值,存取,运算;成员名与类型名可以重名
  • 初始化方法
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach//mycoach是结构体类型,如果省略就是无名结构体
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
}cpc, xxd, wr, fgf;
void main()
{

    struct mycoach cpc = {"陈培昌",22,"泰拳,散打"};
    printf("%s
", cpc.expertin);
    system("pause");
}

#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach//mycoach是结构体类型,如果省略就是无名结构体
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
}cpc = { "陈培昌", 22, "泰拳,散打" };
void main()
{
    printf("%s
", cpc.expertin);
    system("pause");
}
  • 无名结构体,无法访问,无法创建变量,但下列方法可以(业务场景,限量使用该结构体,如银行数据库的最高权限)
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
}cpc;
void main()
{
    sprintf(cpc.name,"陈培昌");
    cpc.age = 22;
    sprintf(cpc.expertin,"泰拳,散打");
    printf("%s
", cpc.expertin);
    system("pause");
}

another form

#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
}cpc = { "陈培昌", 22, "泰拳,散打" };
void main()
{
    printf("%s
", cpc.expertin);
    system("pause");
}

 结构体可以互相赋值

#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    char expertin[100];
}fgf,sample = { "付高峰", 30, "MMA,散打" };
void main()
{
    struct mycoach cpc = { "陈培昌", 22, "泰拳,散打" };
    fgf = sample;
    //cpc = { "陈培昌", 22, "泰拳,散打" };//错误,{}初始化只能在声明变量时,使用
    printf("%s
", fgf.name);
    system("pause");
}

结构体引用:无法整体引用,只能通过变量引用

#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach { //还可以包含其他结构体,其中每个元素的内存都相互独立 char name[30]; int age; char expertin[100]; };
void main()
{
    struct mycoach cpc = { "陈培昌", 22, "泰拳,散打" }; //cpc = { "陈培昌", 22, "泰拳,散打" };//错误,{}初始化只能在声明变量时,使用
    printf("%s
", cpc.expertin);
    system("pause");
}
  • 云计算的原理(最原始的算法)

对于每个搜索查询参数,都根据网页信息,关键词数组,信息的地址,计数;相关排名最高的排在最先

网页信息包含了html数据

卡巴斯基的病毒库,可以理解为用字符串映射一个整数,并构建为树的结构,根据左右孩子结点和父节点的关系来优化搜索效率,如此一来,2^30次方就是10亿条数据,基本用30次就找到了,否则循环遍历就是10亿多次

二叉树的结点,可以被视作一个结构体,其成员包括索引,数据,左结点指针,右结点指针

  • 结构体嵌套
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    struct expertise
    {
        char standing[30];//站立式格斗技
        char groudskill[100];//地面技
    } exp1;//如果声明了结构体变量,将被视作结构体数据的一部分
}fgf,sample = { "付高峰", 30,  };
void main()
{
    struct mycoach cpc = { "陈培昌", 22 };
    sprintf(cpc.exp1.standing,"泰拳");
    sprintf(cpc.exp1.groudskill, "中国式摔跤,MMA");

    //cpc = { "陈培昌", 22, "泰拳,散打" };//错误,{}初始化只能在声明变量时,使用
    printf("%s
", cpc.exp1.groudskill);
    system("pause");
}

  •  结构体数组的声明
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct mycoach
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    struct expertise
    {
        char standing[30];//站立式格斗技
        char groudskill[100];//地面技
    } exp1;//如果声明了结构体变量,将被视作结构体数据的一部分
}fgf[20];//声明为数组

void main()
{
    struct mycoach mcoach[50];//声明一个mycoach结构体数组
    system("pause");
}
  • 对数组成员进行取地址操作
#define _CRT_SECURE_NO_WARNINGS
#define jl struct mycoach
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct mycoach
{
    //还可以包含其他结构体,其中每个元素的内存都相互独立
    char name[30];
    int age;
    struct expertise
    {
        char standing[30];//站立式格斗技
        char groundskill[30];//地面技
    } exp1;//如果声明了结构体变量,将被视作结构体数据的一部分
}fgf[3];//fgf[3] = { {}, {}, {} }; printf("%p",mcoach);

void main()
{
    struct mycoach mcoach[50];//声明一个mycoach结构体数组
    // printf("%p",mcoach);
    // printf("%p",&mcoach[1]);
    // printf("%p",&mcoach[2]);
    strcpy(mcoach[1].name,"付高峰");
    mcoach[1].age = 30;
    strcpy(mcoach[1].exp1.standing, "散打");
    strcpy(mcoach[1].exp1.groundskill, "MMA");
    strcpy(mcoach[0].name, "徐晓冬");
    mcoach[0].age = 40;
    strcpy(mcoach[0].exp1.standing, "散打,拳击");
    strcpy(mcoach[0].exp1.groundskill, "中国式摔跤,MMA");
    printf("%p
",&mcoach[0]);
    printf("%p
",&mcoach[1]);
    system("pause");
}

原文地址:https://www.cnblogs.com/saintdingspage/p/11966293.html