LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1160|回复: 8

一个关于C语言的语法问题

[复制链接]
发表于 2004-11-24 20:36:58 | 显示全部楼层 |阅读模式
原来的问题:
[PHP]
struct lookup {                                 /*一开始没看到这段声明问了个傻问题*/
        const char *        l_word;
        const int        l_value;
};

static struct lookup const *        byword P((const char * string,
                                        const struct lookup * lp));

static struct lookup const        mon_names[] = {
        { "January",        TM_JANUARY },
        { "February",        TM_FEBRUARY },
        { "March",        TM_MARCH },
        { "April",        TM_APRIL },
        { "May",        TM_MAY },
        { "June",        TM_JUNE },
        { "July",        TM_JULY },
        { "August",        TM_AUGUST },
        { "September",        TM_SEPTEMBER },
        { "October",        TM_OCTOBER },
        { "November",        TM_NOVEMBER },
        { "December",        TM_DECEMBER },
        { NULL,                0 }
};

static struct lookup const        wday_names[] = {
        { "Sunday",        TM_SUNDAY },
        { "Monday",        TM_MONDAY },
        { "Tuesday",        TM_TUESDAY },
        { "Wednesday",        TM_WEDNESDAY },
        { "Thursday",        TM_THURSDAY },
        { "Friday",        TM_FRIDAY },
        { "Saturday",        TM_SATURDAY },
        { NULL,                0 }
};
[/PHP]

请问这种struct xxxx const xxxx[]={} 是什么用法?
解答:这是因为个人失误 没有看到对结构体 lookup 的定义,这两个只是lookup结构体的2个常数型变量数组。

问题来了 : 这个函数声明真奇怪,哪位仁兄可以解释一下
[php]static struct lookup const *        byword P((const char * string,
                                        const struct lookup * lp));
[/php]
发表于 2004-11-24 20:41:07 | 显示全部楼层
struct是结构体
const好像是共用体的说
具体用法,我想任何一本C语言教程上,都比我说的清除详细
发表于 2004-11-24 20:53:01 | 显示全部楼层
只是定义了一个数组
就如同
  1. int a[]={1,2,3,4};
  2. char c[]={'a','b','c','d'};
复制代码
 楼主| 发表于 2004-11-24 21:17:03 | 显示全部楼层
不好意思各位  这段代码前边还有这么一段
[php]
struct lookup {
        const char *        l_word;
        const int        l_value;
};
[/php]
刚刚 我没注意到

现在的问题是
[php]static struct lookup const *        byword P((const char * string,
                                        const struct lookup * lp));

[/php]
是什么作用
发表于 2004-11-24 21:37:10 | 显示全部楼层
最初由 马桶 发表
static struct lookup const *        byword P((const char * string,
                                        const struct lookup * lp));
这个和你原来的可是不一样的,原来的代码就是定义数组。
  1. static struct lookup const    wday_names[] = {
  2.     { "Sunday",    TM_SUNDAY },
  3.     { "Monday",    TM_MONDAY },
  4.     { "Tuesday",    TM_TUESDAY },
  5.     { "Wednesday",    TM_WEDNESDAY },
  6.     { "Thursday",    TM_THURSDAY },
  7.     { "Friday",    TM_FRIDAY },
  8.     { "Saturday",    TM_SATURDAY },
  9.     { NULL,        0 }
  10. };
复制代码
就是定义了一个名为wday_names的数组,数组元素类型是static struct lookup const
{ "Sunday",    TM_SUNDAY },等等是数组元素的初始值
想想 int a[]={1,2,3,4}
发表于 2004-11-24 21:41:22 | 显示全部楼层
这是定义一个链表,const是不允许改变的意思,static是静态变量的意思吧,
发表于 2004-11-24 21:50:14 | 显示全部楼层
晕,const是常量的意思,只能读不能写。
static的作用有好几种,这里的作用是限制这个函数的作用域
static struct lookup const    mon_names[] = { };结构数组

static struct lookup const *    byword P((const char * string,
                    const struct lookup * lp));
声明一个返回值是指针的函数,
发表于 2004-11-24 22:09:27 | 显示全部楼层
何来链表
发表于 2004-11-25 10:34:02 | 显示全部楼层
[php]
static struct lookup const *    byword P((const char * string,
                    const struct lookup * lp));
[/php]
可以一步一步地分解开来看。
P((...))应该是一个宏。主要用于保持函数原型的兼容性。展开后应该是
[php]
           byword(const char * string, const struct lookup * lp);
[/php]
再加上前面返回值的类型。完整的原型应该是:
[php]
static struct lookup const * byword(const char * string, const struct lookup * lp); [/php]
这说明byword有一个类型为const char* 的参数,一个类型为const struct lookup *的参数。函数返回值的类型为struct lookup const *,是一个指向lookup结构的常量指针。前面的static是用于限制函数名在编译时只在本编译单位内有效的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表