c++ - #define myptr int * 有什么问题?

标签 c++ macros

这是一小段代码:

#include <iostream>
using namespace std;

#define myptr int *

int main(){
    myptr p,q;
    int c;
    p = &c;
    q = &c;
    c = 2;
    cout<<c<<endl;
    cout<<*p<<endl;
    cout<<*q<<endl;

    return 0;
}

p 的类型为 int*q 的类型为 int。为什么会这样?

最佳答案

宏替换后,myptr p,q;变为

int *p,q;

认为q 的类型是int* 是一个常见的错误,但实际上它是int

你需要的是typedef:

typedef int* myptr;

关于c++ - #define myptr int * 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886410/

相关文章:

c - 宏中字符串和数字的混合

c++ - 实现登录功能或宏可以记录日期和功能名称

c# - 创建内联函数和宏

c - 如何将可变参数函数调用作为宏定义?

c++ - 如何在不拖延主循环的情况下实现对话系统?

c++ - Templated Defaulted 默认构造函数

c++ - 如何创建语音聊天应用程序(SIP 协议(protocol))

c++ - 使用 OpenMP 获得完整、未损坏的输出需要什么?

c++ - 使用 sprintf 打印十六进制值的便携方式

macros - haxe:从haxe.macro.Position获取行号和行位置