楼主:
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 的薪水一样, 易读而已