指標指向結構
宣告指標
結構 指標變數 = 結構變數地址;
Student *ptr = &student;
存取結構成員
使用取值運算子
(*指標變數).成員
完整程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
typedef struct{
//學生姓名
char* name;
//學號
int id;
}Student;
int main() {
Student student = {"marry", 1};
Student *ptr = &student;
cout << "姓名 : " << (*ptr).name << endl;
cout << "學號 : " << (*ptr).id << endl;
return 0;
}
使用->
指標變數->成員
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
typedef struct{
//學生姓名
char* name;
//學號
int id;
}Student;
int main() {
Student student = {"marry", 1};
Student *ptr = &student;
cout << "姓名 : " << ptr->name << endl;
cout << "學號 : " << ptr->id << endl;
return 0;
}