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

版权声明:欢迎转载 https://blog.csdn.net/stone_fall/article/details/88363197

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


目录

结构体

结构体一般形式:

定义一个结构体:

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

结构体数组:

结构体数组初始化与赋值

结构体构造函数

第一种构造函数形式

第二种构造函数形式


结构体

结构体一般形式:

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类型的数组、指针的应用,我一直不明白,所以只写了上述我明白的部分内容,如有弄清楚的童鞋,请贴个链接或者留言告诉我,不甚感激

猜你喜欢

转载自blog.csdn.net/stone_fall/article/details/88363197
今日推荐