初始化結構

使用大括號{}初始化

結構  變數名 = {值1, 值2 ...}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char* name;
  //學號
  int id;
}Student;
int main() {
  Student student = {"Mary", 1};
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}
Mary
1

定義結構時初始化

struct 結構名{
	變數宣告
}結構變數={值1, 值2 ...};
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
struct Student{
  //學生姓名
  char* name;
  //學號
  int id;
}student = {"Bill", 2};
int main() {
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}
Bill
2

結構成員記憶體位址的值全初始化成00000000

方式1

{0}把成員初始化00000000。

struct 結構名{
	變數宣告
}結構變數={0};
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
struct Student{
  //學生姓名
  char* name;
  //學號
  int id;
}student = {0};
int main() {
  return 0;
}
結構  變數名 = {0}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char* name;
  //學號
  int id;
}Student;
int main() {
  Student student = {0};
  return 0;
}

方式2

只寫大括號也是把成員初始化00000000。

struct 結構名{
	變數宣告
}結構變數={};
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
struct Student{
  //學生姓名
  char* name;
  //學號
  int id;
}student = {};
int main() {
  return 0;
}
結構  變數名 = {}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char* name;
  //學號
  int id;
}Student;
int main() {
  Student student = {};
  return 0;
}

方式3

c11之徫可以省略等於=

struct 結構名{
	變數宣告
}結構變數{};
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
struct Student{
  //學生姓名
  char* name;
  //學號
  int id;
}student{};
int main() {
  return 0;
}
結構  變數名{}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char* name;
  //學號
  int id;
}Student;
int main() {
  Student student{};
  return 0;
}

方式4

使用memset()

memset(結構變數地址,0,sizeof(結構變數));
1
2
3
4
5
int main() {
  Student student;
  memset(&student, 0, sizeof(student));
  return 0;
}

results matching ""

    No results matching ""