Re: 請教C++與結構
※ 引述《jiannrong@kkcity.com.tw ( )》之銘言:
> 請教一下,為何
> person1.name2="Mary";
> 會有問題呢?
> 謝謝
> 附上原始程式碼:
> #include <iostream>
> using namespace std;
> main( )
> {
> struct company //結構
> {
> char name2[20];
> int salary;
> int prize;
> int total;
> };
> struct company person1;
> person1.name2="Mary";
> person1.salary=20000;
> person1.prize=1500;
> person1.total=person1.salary + person1.prize;
> cout << "姓名:" << person1.name2 <<endl;
> cout << "薪資:" << person1.salary<<endl;
> cout << "獎金:" << person1.prize<<endl;
> cout << "總共:" << person1.total<<endl;
> // system("PAUSE");
> return 0; }
g++後的錯誤訊息
error: incompatible types in assignment of `const char[5]' to `char[20]'
person1.name2本身是陣列起始位址,沒法做指定運算
試看看
strcpy(person1.name2,"Mary"); // #include <cstring>
--
夫兵者不祥之器物或惡之故有道者不處君子居則貴左用兵則貴右兵者不祥之器非君子
之器不得已而用之恬淡為上勝而不美而美之者是樂殺人夫樂殺人者則不可得志於天下
矣吉事尚左凶事尚右偏將軍居左上將軍居右言以喪禮處之殺人之眾以哀悲泣之戰勝以
喪禮處之道常無名樸雖小天下莫能臣侯王若能守之萬物將自賓天地相合以降甘露民莫
之令而自均始制有名名亦既有夫亦將知止知止可以不殆 Kino.Dorm10.NCTU.edu.tw海
推
04/24 10:17, , 1F
04/24 10:17, 1F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 4 篇):