指针初始化无法识别定义的类型

时间:2017-01-04 15:44:09

标签: c pointers c-preprocessor typedef

尝试定义用户类型我偶然发现错误我不太明白:以下代码正常运行

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并不会改变行为。

1 个答案:

答案 0 :(得分:1)

#define不同,#define NEW_SYMBOL [old_symbol]的语法为typedeftypedef existing_type new_type; 的语法为:

typedef const int constant;

所以:

typedef constant const int;

是有效的C,而

.reduce()

不是。

相关问题