沉静
  
  首页 >>  
我的日历
分类日志
友情链接
最新评论
搜索日志
访问计数
获取 RSS
我的 Blog:
youalwayscan 最新的 20 条日志
[心情留言]
[点滴积累]
[好文共赏]
[C/C+基础]
[Unix/Linux基础]
[WxWidgets]
[VC/MFC]
全站 Blog:
全站最新的 20 条日志

 
struct's being used [2005-4-19]
youalwayscan 发表在 C/C+基础
Consider writing a program to count the occurrences of each C keyword. We need an array of character strings to hold the names, and an array of integers for the counts. One possibility is to use two parallel arrays, keyword and keycount, as in
char *keyword[NKEYS];
int keycount[NKEYS];

But the very fact that the arrays are parallel suggests a different organization, an array of structures. Each keyword is a pair:
char *word;
int cout;

and there is an array of pairs. The structure declaration
struct key {
char *word;
int count;
} keytab[NKEYS];

declares a structure type key, defines an array keytab of structures of this type, and sets aside storage for them. Each element of the array is a structure.
≡≡≡≡≡ 评论(共 条)我要评论