結構變數經過宣告後
就可以使用設定運算子(=)來設定結構的初值
Ex:
[code language=”cpp”]
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
string id;
int math;
int eng;
};
int main() {
struct student s1={"Tony","AMA104143",70,60}; //宣告結構變數時即可設定初值
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
當然也可以在宣告結構時
即宣告結構變數以及設定初值
Ex:
[code language=”cpp”]
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
string id;
int math;
int eng;
}s1={"Tony","AMA104143",70,60}; //宣告結構變數時即可宣告變數
//並且設定初值
int main() {
}
[/code]