尝试定义用户类型我偶然发现错误我不太明白:以下代码正常运行
const int a[5] = { 1, 2, 3, 4, 5 };
typedef const int costant;
costant b = 5;
costant *c = a;
void func( const int item )
{
printf("Item: %i\n", item );
}
int main( void )
{
func( a[0] );
func( b );
func( *c );
}
Nonethe less,初始化指针
costant *c = a[0];
未能说明Initializer element is not costant
。尝试访问位置a[0]
以更改其内容将被拒绝,错误为assignment of read-only location
。
为什么将数组地址a
视为const而不是其元素a[0]
,即使它是"只读"?
用typedef
替换#define costant const int
并不会改变行为。
答案 0 :(得分:1)
与#define
不同,#define NEW_SYMBOL [old_symbol]
的语法为typedef
,typedef existing_type new_type;
的语法为:
typedef const int constant;
所以:
typedef constant const int;
是有效的C,而
.reduce()
不是。