結構與字串

Prerequisites:

結構成員與char字串

以下結構成員name為char字串。

1
2
3
4
5
6
typedef struct{
  //學生姓名
  char name[100];
  //學號
  int id;
}Student;

使用char字串,不能使用等於=指定值,以下語法將編譯錯誤。

1
student.name = "Mary";

正確使用的方式是使用strcpy拷貝字串到char字串。

1
strcpy(student.name, "Mary");

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char name[100];
  //學號
  int id;
}Student;
int main() {
  Student student;
  //清空資料
  //student記憶體位址的值全設為00000000
  memset(&student, 0, sizeof(student));
  //姓名
  strcpy(student.name, "Mary");
  //學號
  student.id = 100;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}
Mary
100

結構成員與char字串指標

以下結構成員name為char字串指標。

1
2
3
4
5
6
typedef struct{
  //學生姓名
  char* name;
  //學號
  int id;
}Student;

使用char字串指標,可以使用等於=指定值。

1
student.name = "Mary";

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char* name;
  //學號
  int id;
}Student;
int main() {
  Student student;
  //清空資料
  //student記憶體位址的值全設為00000000
  memset(&student, 0, sizeof(student));
  //姓名
  student.name = "Mary";
  //學號
  student.id = 100;
  cout << student.name << endl;
  cout << student.id << endl;
  return 0;
}
Mary
100

結構成員與char字串與字串指標(c11)

使用大括號的方式,不管是字串或字串指標都可以用雙引號”“指定值。

1
student = {"Mary","桃園市XX區XX號", 1};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char name[100];
  //地址
  char* address;
  //學號
  int id;
}Student;
int main() {
  Student student;
  student = {"Mary","桃園市XX區XX號", 1};
  cout << student.name << endl;
  cout << student.address << endl;
  cout << student.id << endl;
  return 0;
}
Mary
桃園市XX區XX號
1

無法使用大括號修改單獨成員是字串陣列

以下的寫法會編譯錯誤

1
student.name = {"Bill"};

必須使用strcpy修改單獨成員是字串陣列

1
strcpy(student.name, "Bill");

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
typedef struct{
  //學生姓名
  char name[100];
  //地址
  char* address;
  //學號
  int id;
}Student;
int main() {
  Student student;
  student = {"Mary","桃園市XX區XX號", 1};
  strcpy(student.name, "Bill");
  cout << student.name << endl;
  cout << student.address << endl;
  cout << student.id << endl;
  return 0;
}
Bill
桃園市XX區XX號
1

results matching ""

    No results matching ""