问:b的类型是什?
typedef char * a;
const a b;
const a b;
答: b的类型是 char * const
类型声明的时候有一个type specifier. 基本类型都属于这个范围, 比如char, int, long.
比较特别的是 struct, union, enum还有typedef都属于type specifier. 所以在给type specifier加const修饰符的时候, 是加在struct, union, enum和typedef上面的, 就和加在int上面一样.
所以如果
typedef int * a;
a是一个typdef后的类型, 具体点讲是指向int的指针. 这时候如果有const a b, 这里的a是作为type spcifier出现, const会加在a上面, 所以b就成了const的指向int的指针.
还有比较特别的是const加在struct或者union上的时候.
如果有
struct a
{
int b;
const int c;
};
那么const struct a b;相当于把const加在struct a的每个items上, 此时的b相当于
struct c
{
const int b;
const int c;
} b;
类型声明的时候有一个type specifier. 基本类型都属于这个范围, 比如char, int, long.
比较特别的是 struct, union, enum还有typedef都属于type specifier. 所以在给type specifier加const修饰符的时候, 是加在struct, union, enum和typedef上面的, 就和加在int上面一样.
所以如果
typedef int * a;
a是一个typdef后的类型, 具体点讲是指向int的指针. 这时候如果有const a b, 这里的a是作为type spcifier出现, const会加在a上面, 所以b就成了const的指向int的指针.
还有比较特别的是const加在struct或者union上的时候.
如果有
struct a
{
int b;
const int c;
};
那么const struct a b;相当于把const加在struct a的每个items上, 此时的b相当于
struct c
{
const int b;
const int c;
} b;