作者:
s8604561 (andy821201)
2016-03-25 21:06:55剛剛讀到有關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 程式的執行結果是一樣的
所以想知道這兩種寫法是否會有差異
作者: PRAM (PRAM) 2016-03-26 23:41:00
typedef struct test score 意思是幫 test 結構取個別名叫作score 而已, 就像有人會用 typedef int salary 這樣把 int取個別名叫 salary , 然後就用 salary jack ; 去宣告 jack的薪水和你直接用 int jack ; 來代表 jack 的薪水一樣, 易讀而已