在 typedef 声明中使用逗号? [英] Use of comma in a typedef declaration?

查看:80
本文介绍了在 typedef 声明中使用逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此声明是否符合 C99/C11 标准?

Is this declaration C99/C11 compliant ?

typedef struct element {
    char *data; 
    struct element* next;
} element, *list, elements[5];

我找不到它在标准中工作的原因.

I could not find why it works in the standard.

推荐答案

是的,它符合标准.typedef 声明类似于普通声明,除了它们声明的标识符成为对象类型的别名,如果声明中没有 typedef.

Yes, it is standard compliant. typedef declarations are like normal declarations except the identifiers declared by them become type aliases for the type of object the identifier would be if the declaration had no typedef in it.

那么一会儿

int integer, *pointer_to_integer;

声明一个名为integerint对象和一个名为pointer_to_integer

declare an int object named integer and an int * object named pointer_to_integer

typedef int integer, *pointer_to_integer;

将声明一个名为 integerint 类型别名和一个名为 pointer_to_integerint * 类型别名>.

would declare an int-typed type alias named integer and an int *-typed type alias named pointer_to_integer.

从语法的角度来看,虽然不是功能性的,typedef 只是一个(假的)存储分类器(如 externauto>staticregister_Thread_local).

From a syntactical perspective, though not functionally, typedef is just a (fake) storage classifier (like extern, auto, static, register or _Thread_local).

您的声明

typedef struct element {
    char *data; 
    struct element* next;
} element, *list, elements[5];

稍微复杂一点,因为它也定义struct element数据类型,但它等价于:

is slightly more complicated because it also defines the struct element data type but it's equivalent to:

struct element {
    char *data; 
    struct element* next;
}; 
// <= definition of the `struct element` data type
// also OK if it comes after the typedefs
// (the `struct element` part of the typedef would then
// sort of forward-declare the struct )

/*the type aliases: */
typedef struct element 
      element, *list, elements[5];

-- 将element声明为struct element的类型别名,list声明为struct element的类型别名*elements 作为 struct element [5] 的类型别名.

-- it declares element as a type alias to struct element, list as a type alias to struct element * and elements as a type alias to struct element [5].

这篇关于在 typedef 声明中使用逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆