69,337
社区成员
发帖
与我相关
我的任务
分享
typedef int * PINT;
PINT m, n; // 此时,m, n均为(int * )类型。
m = n; // 可以,同为int * 类型的指针
#define PINT int *
PINT m, n; // 此时,m为(int *)类型,而n为(int)类型。因为宏展开后,该行实际为 int * m, n;
m = n; // 编译器会警告类型不匹配
m = &n; // 可以
[/quote]
谢谢你们的耐心讲述,我差不多明白了typedef和#define的区别,我不理解的是下面,这个在C++primer中也讲过,不是特别理解,还能相信讲一下么
typedef int * pint;
const pint x;
pint 不会拆开为 int *
然后和 const 组合成 const int *
而是 作为一个int *类型
那么 const pint 就是 pint const, 也就是 int *const;
为啥 const pint 就成了 int *const; 因为pint 不会拆开为 int * 他这样确实就是一个int指针为啥被const修饰之后就成了 int *const;[/quote]
对一个类型 T
const T a;
和
T const a;
都是 定义 T 类型常量
int *const a; //a 这个int类型指针是常量;这是指针常量,指针本是是常量。
const int *a; // *a;这个 int 类型的对象是常量。a这是常量指针。指向常量的指针
//单独的const int * 这三个符号放在一起,编译器会根据运算符结合性,优先级,
以及各个符号的顺序,解释定义(声明)的对象的类型 。
const pint a; /// pint 对象a, 也就是 int 指针(int*)a, 是个常量; 这就是类型 pint 不再拆分为 int *两个符号的含义。
/// pint 是 int 指针 (int*)最先得到解释 ,不再是 const int * 三个符号,放在一起处理后解释了。
pint const a; ///a 是个 int 指针(int*) 并且是指针常量,也是先解释 pint 再解释 pint const ;
这样,pint const a; int *const a;定义的对象,就是同一个类型的对象了。这算是巧合。
int *const a; 是四个符号(包括标识符 a), 一起解释。
这个语句解释成定义指针常量 a;
这样 int *const 这个类型,就是指针常量类型;
[/quote]
更深的理解了下#与##在宏定义中的--宏展开
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n", h(f(1,2))); // 12
printf("%s\n", g(f(1,2))); // f(1,2)
return 0;
}
宏展开时:
如果宏定义以#开头,不展开参数,直接替换。
故g(f(1,2))--->#f(1,2)--->"f(1,2)";
如果宏定义不以#开头,展开参数,直接替换,由外层向里层,如果碰到的是#开头的宏,不继续往里层展开,往外层展开。
由外层向里层,如果碰到的是以非#开头的宏,继续往里层走,直至最里层,开始一层层往外层展开。
故h(f(1,2))--->h(12)--->g(12)---->#12----->"12"。
PS:
##在宏中定义,是字符连接符
如a##b##c 等同于 "abc"
#在宏开头出现,是表示宏展开的方式不同
#a 等同于"a"
#abc 等同于 "abc"
复杂的:
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
char a = 'a';
cout<<g(a)<<endl; // a
cout<<g(g(a))<<endl; // a
printf("%s\n", h(f(1,2))); // 12
printf("%s\n", g(f(1,2))); // f(1,2)
printf("%s\n", g(h(f(1,2)))); // h(f(1,2))
printf("%s\n", h(g(f(1,2)))); // "f(1,2)"
printf("%s\n", h(h(f(1,2)))); // "12"
system("pause");
return 0;
}
预处理后的:(在编译选项中添加/EP /P后编译生成的.i文件。gcc加-E)
int main()
{
char a = 'a';
cout<<"a"<<endl;
cout<<"g(a)"<<endl;
printf("%s\n", "12");
printf("%s\n", "f(1,2)");
printf("%s\n", "h(f(1,2))");
printf("%s\n", "\"f(1,2)\"");
printf("%s\n", "\"12\"");
system("pause");
return 0;
}
---------------------------------------------------
宏解析
1. ##操作符
##操作符它的作用是在替代表中将其前后的参数连接成为一个预处理符号,它不能出现于宏替代表的开端和末尾。
例:
#define concat(s,t) s##t
#define AAA ABC
concat(A, AA)
将被替换成
ABC
2. 重新扫描和替换
在替换列表中的所有参数替换过之后,预处理器将对结果token序列重新扫描以便对其中的宏再次替换。
当正在替换的宏在其替换列表中发现自身时,就不再对其进行替换。在任何正在嵌套替换的宏的替换过程中遇到正被替换的宏就对其不再进行替换(防止递归)。
例:
#define ROOT AAA CCC
#define AAA ROOT
ROOT
将被替换成
ROOT CCC
typedef是定义一个别名
typedef
typedef type-declaration synonym;
The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition.
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.
Example
// Example of the typedef keyword
typedef unsigned long ulong;
ulong ul; // Equivalent to "unsigned long ul;"
typedef struct mystructtag
{
int i;
float f;
char c;
} mystruct;
mystruct ms; // Equivalent to "struct mystructtag ms;"
typedef int (*funcptr)(); // funcptr is synonym for "pointer
// to function returning int"
funcptr table[10]; // Equivalent to "int (*table[10])();"
typedef int * PINT;
PINT m, n; // 此时,m, n均为(int * )类型。
m = n; // 可以,同为int * 类型的指针
#define PINT int *
PINT m, n; // 此时,m为(int *)类型,而n为(int)类型。因为宏展开后,该行实际为 int * m, n;
m = n; // 编译器会警告类型不匹配
m = &n; // 可以
[/quote]
谢谢你们的耐心讲述,我差不多明白了typedef和#define的区别,我不理解的是下面,这个在C++primer中也讲过,不是特别理解,还能相信讲一下么
typedef int * pint;
const pint x;
pint 不会拆开为 int *
然后和 const 组合成 const int *
而是 作为一个int *类型
那么 const pint 就是 pint const, 也就是 int *const;
为啥 const pint 就成了 int *const; 因为pint 不会拆开为 int * 他这样确实就是一个int指针为啥被const修饰之后就成了 int *const;[/quote]
对一个类型 T
const T a;
和
T const a;
都是 定义 T 类型常量
int *const a; //a 这个int类型指针是常量;这是指针常量,指针本是是常量。
const int *a; // *a;这个 int 类型的对象是常量。a这是常量指针。指向常量的指针
//单独的const int * 这三个符号放在一起,编译器会根据运算符结合性,优先级,
以及各个符号的顺序,解释定义(声明)的对象的类型 。
const pint a; /// pint 对象a, 也就是 int 指针(int*)a, 是个常量; 这就是类型 pint 不再拆分为 int *两个符号的含义。
/// pint 是 int 指针 (int*)最先得到解释 ,不再是 const int * 三个符号,放在一起处理后解释了。
pint const a; ///a 是个 int 指针(int*) 并且是指针常量,也是先解释 pint 再解释 pint const ;
这样,pint const a; int *const a;定义的对象,就是同一个类型的对象了。这算是巧合。
int *const a; 是四个符号(包括标识符 a), 一起解释。
这个语句解释成定义指针常量 a;
这样 int *const 这个类型,就是指针常量类型;
const int m = 0; // 告诉编译器,m 为一个int类型的常量
m = 1; // 编译器会报错,因为m这个int数值不能(应)修改
当你把'int'替换为 'PINT'时,
int n;
const PINT m = NULL; // 告诉编译器,m 为一个PINT类型的常量, 即m这个int *指针是个常量
// const 修饰的是PINT, 相当于是“ const (int *) ”, 不过C中没有这种写法, 等价的写法是int * const
m = &n; //编译器会报错,因为 m 这个指针值不能(应)修改
typedef int * PINT;
PINT m, n; // 此时,m, n均为(int * )类型。
m = n; // 可以,同为int * 类型的指针
#define PINT int *
PINT m, n; // 此时,m为(int *)类型,而n为(int)类型。因为宏展开后,该行实际为 int * m, n;
m = n; // 编译器会警告类型不匹配
m = &n; // 可以
[/quote]
谢谢你们的耐心讲述,我差不多明白了typedef和#define的区别,我不理解的是下面,这个在C++primer中也讲过,不是特别理解,还能相信讲一下么
typedef int * pint;
const pint x;
pint 不会拆开为 int *
然后和 const 组合成 const int *
而是 作为一个int *类型
那么 const pint 就是 pint const, 也就是 int *const;
为啥 const pint 就成了 int *const; 因为pint 不会拆开为 int * 他这样确实就是一个int指针为啥被const修饰之后就成了 int *const;