作者s8604561 (andy821201)
看板C_and_CPP
標題[討論] 有關struct
時間Fri Mar 25 21:06:55 2016
剛剛讀到有關struct的問題
#include <stdio.h>
#include <stdlib.h>
void main(){
struct test{
int math;
int english;
};
typedef struct test score;
score joe, jane;
/* test joe, jane;*/
joe.math = 80;
joe.english = 75;
jane.math = 95;
jane.english = 68;
printf("%d\n", joe.math);
printf("%d\n", joe.english);
printf("%d\n", jane.math);
printf("%d\n", jane.english);
system("pause");
}
想請問各位前輩
typedef struct test score
score joe,jane;
這段的用意是甚麼呢?
我用
test joe,jane;
也是可以達到一樣的執行結果
我看書上宣告test joe,jane 前面都會加上struct的關鍵字
所以說用typedef 重新定義後就可以不用每次都要加struct
不過我測試後不加struct 程式的執行結果是一樣的
所以想知道這兩種寫法是否會有差異
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.42.43.137
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1458911217.A.C19.html
推 bibo9901: C 需要用 struct test , C++ 不用 03/25 21:13
推 tuyutd0505: C++ 以後就基本沒差別了 03/25 21:41
推 PRAM: typedef struct test score 意思是幫 test 結構取個別名叫作 03/26 23:41
→ PRAM: score 而已, 就像有人會用 typedef int salary 這樣把 int 03/26 23:42
→ PRAM: 取個別名叫 salary , 然後就用 salary jack ; 去宣告 jack 03/26 23:42
→ PRAM: 的薪水 03/26 23:43
→ PRAM: 和你直接用 int jack ; 來代表 jack 的薪水一樣, 易讀而已 03/26 23:44
→ james732: 可是我很討厭同一個東西有好幾個名字XDDD 03/27 15:29
→ Schottky: 有時候取別名是因為把舊有struct或 data type 挪作他用 03/28 02:15
→ Schottky: 為了讓它在新用途上名符其實才需要另取個新名字 03/28 02:15
→ s8604561: 看了P大的回覆 讓我懂了用這個的用意 感謝p大 03/28 15:42
→ jaid: 說穿了就是抽象化 03/28 16:28