小编Sea*_*son的帖子

将指针传递给函数 - 为什么我不能打印地址?

我不明白为什么我不能打印指针的地址.我知道理解指针是非常基础的,所以任何帮助都会受到赞赏.

void printp(int& test)
{
        cout << test << endl;
}

int main()
{
        int *p = new int;
        *p = 1;
        int np = 0;

//      printp(p);  // why doesn't this print the address of p?
//      printp(&p); // why doesn't this print the address of p?
        printp(*p); // prints 1
        printp(np); // prints 0

return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用'printp(p)'时出现以下错误.

test.cpp: In function ‘int main()’:
test.cpp:17:10: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
  printp(p);  // why doesn't this print …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

3
推荐指数
1
解决办法
108
查看次数

标签 统计

c++ ×1

pointers ×1