C | struct结构体 | 结构体数组 | typedef struct | 结构体初始化 | 结构体构造函数 | 结构体嵌套

本人小白,有错误之处恳请指出,感激不尽


目录

结构体

结构体一般形式:

定义一个结构体:

结构体初试化的一般方法:

结构体数组:

结构体数组初始化与赋值

结构体构造函数

第一种构造函数形式

第二种构造函数形式


结构体

结构体一般形式:

struct 结构名{
	//成员列表 
};//分号必不可少

定义一个结构体:

struct stu{
	int num;
	string name1;
	char name2[20];
	float core;
}s1;
//s1是一个变量

struct stu s1;//同

结构体初试化的一般方法:

以学生结构举例:

  • 方法一
#include<stdio.h>
struct stu{
	int num;
	char name[20];
	float core;
};
int main()
{
    struct stu s1={1,"Li Hua",89.0};
    printf("%d %s %f\n",s1.num,s1.name,s1.core);
    return 0;
}
  • 方法2
#include<bits/stdc++.h>
using namespace std;
struct stu{
	int num;
	string name1;
	char name2[20];
	float core;
};
int main()
{
    struct stu s1;
    s1.num=1;
    s1.name1="Li Ming";
    strcpy(s1.name2,"Li Ming");//不能像string一样赋值,会报错
    s1.core=99;
    printf("%d\n%s\n%s\n%f\n",s1.num,s1.name1.c_str(),s1.name2,s1.core);
    return 0;
}

结构体数组:

定义stu型的有100个存储空间的s数组

  • 方法一
struct stu{
	int num;
	char name[20];
	int age;
}s[100];
  • 方法二
struct stu{
	int num;
	char name[20];
	int age;
};
int main(){
	struct stu s[100];
}

结构体数组初始化与赋值

  • 方法一:先定义再赋值
#include<bits/stdc++.h>
using namespace std;
typedef struct stu{
	int num;
	char name2[20];
	float core;
}Stu;
int main()
{
    Stu s[2];
    s[0].num = 1;
    strcpy(s[0].name2,"LiMing");
    s[0].core= 99.00;
    s[1].num = 2;
    strcpy(s[1].name2,"LiHua");
    s[1].core = 90.00;
    for(int i=0; i<2; i++){
        printf("%d\n%s\n%f\n",s[i].num,s[i].name2,s[i].core);
    }
    return 0;
}
  • 方法二:定义时初始化
#include<bits/stdc++.h>
using namespace std;
typedef struct stu{
	int num;
	char name2[20];
	float core;
}Stu;
int main()
{
    Stu s[2]={{1,"Li Ming",99},{2,"Li Hua",98}};
    for(int i=0; i<2; i++){
        printf("%d\n%s\n%f\n",s[i].num,s[i].name2,s[i].core);
    }
    return 0;
}

结构体构造函数

第一种构造函数形式

struct Stu{
    //成员列表
    Stu(形参){
    //赋值
    }
}
  • 方法一
#include<bits/stdc++.h>
using namespace std;
typedef struct stu{
	int num;
	char name[20];
	float core;
	stu(){
	}
	stu(int _num,char _name[20],float _core){
	    num = _num;
	    strcpy(name,_name);
	    core = _core;
	}
}Stu;
int main()
{
    Stu s[2];
    return 0;
}
  • 方法二
#include<bits/stdc++.h>
using namespace std;
typedef struct stu{
	int num;
	char name[20];
	float core;
	stu(int _num,char _name[20],float _core){
	    num = _num;
	    strcpy(name,_name);
	    core = _core;
	}
}Stu;
int main()
{
    char name1[20]="LiMing";
    char name2[20]="LiHua";
    Stu s[2] = {stu(1,name1,99.00),stu(2,name2,90.00)};
    for(int i=0; i<2; i++){
        printf("%d\n%s\n%f\n",s[i].num,s[i].name,s[i].core);
    }
    return 0;
}

第二种构造函数形式

  • 不带默认参数
#include<bits/stdc++.h>
using namespace std;
struct Stu{
	int num;
	char *name;
	float core;
	Stu(int num,char *name,float core):num(num),name(name),core(core){}
};
  • 带默认参数
struct Stu{
	int num;
	float core;
	Stu(int num=0,float core=0):num(num),core(core){}
};

结构体对于char类型的数组、指针的应用,我一直不明白,所以只写了上述我明白的部分内容,如有弄清楚的童鞋,请贴个链接或者留言告诉我,不甚感激

嵌套结构体以及构造函数

#include <iostream>
using namespace std;
struct _edge;
typedef struct _node{
    int val;
    _node *next;

    struct _ddata{
        _edge  *children;
    }ddata;

    _node(int x):val(x),next(NULL){
        ddata.children = NULL;
    }
}node;
typedef struct _edge{
    int val;
    _node *parent,*child;
    _edge *nextchild;
    _edge **prevchild;
    _edge(int x):val(x),parent(NULL),child(NULL),nextchild(NULL),prevchild(NULL){}
}edge;
int main(){
    node *n1 = new node(1);
    node *n2 = new node(2);
    node *n3 = new node(3);
    edge *e1 = new edge(1);
    edge *e2 = new edge(2);
    e1->parent=n1;
    e1->child=n2;
    e2->parent=n1;
    e2->child=n3;
    n1->ddata.children=e1;
    e2->nextchild=n1->ddata.children;
    e2->nextchild->prevchild = &(e2->nextchild);
    return 0;
}

 

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值