struct Food{
int price;
int taste;
};
double CP(Food somefood){
return (double)somefood.taste / somefood.price;
}
int hp = 50, mp = 25;
int *hp_ptr = &hp; // hp_ptr == 0x69fef8
hp_ptr == &hp; // true
*hp_ptr == hp; // true
hp_ptr == &*hp_ptr; // true
hp == *&hp; // true
void drinkwater(int *mp_ptr){
*mp_ptr += 100;
}
Food hamburger = ({80, 75});
Food *lunch = &hamburger;
int main(){
Food taida_cafeteria = {50, 59};
Food taida_bakery = {30, 60};
Food macdonalds = {119, 87};
Food *myfavorite = &macdonalds;
std::cout << (*myfavorite).price << std::endl;
}
struct Person{
std::string name;
Person mother;
Person father;
};
int main(){
//...
std::cout << me.mother.name << std::endl;
}
error: field 'mother' has incomplete type 'Person'
struct Person{
std::string name;
Person *mother;
Person *father;
};
int main(){
//...
std::cout << (*(*me).mother).name << std::endl;
}
每次都一堆點點星星好麻煩~
std::cout << (*(*me).mother).name << std::endl;
std::cout << me->mother->name << std::endl;
資訊之芽目前有A~F 6位講師
A最好的朋友為B、B最好的朋友為C ... F最好的朋友為A
給一個"講師的指標",輸出他朋友的朋友的名字
friend
是 C++ 的關鍵字,請不要使用struct Lector{
char name;
Lector *bestfriend;
};
int main(){
Lector sprout_lectors[6];
for(int i=0; i<6; i++){
sprout_lectors[i].name = 'A' + i;
sprout_lectors[i].bestfriend = &sprout_lectors[(i+1)%6];
}
Lector *ptr = /*sometesting*/;
std::cout << ptr->bestfriend->bestfriend->name << std::endl;
}
資訊之芽在學長姊的廣告下,學生人數不斷增加。
時不時就有人介紹新同學給大家認識。
struct Student{
std::string name;
Student *introducer;
};
int main(){
Student firststudent = {"Alice", NULL};
Student *thelaststudent = &firststudent, *temp;
std::string name;
while( std::cin >> name){
temp = new Student({name, thelaststudent});
thelaststudent = temp;
}
}
int *ptr = new int;
int *ptr = new int(520);
int *ptr = new int[520];
delete ptr;
delete [] ptr;