c++ - 指针中(星号)的确切用途是什么?

标签 c++ pointers memory-address dereference

我是编程新手,我正在努力思考“指针”的概念。


int main()
{
    int x = 5;
    int *pointerToInteger = & x;
    cout<<pointerToInteger;

}

为什么当我cout << pointerToInteger;输出是一个十六进制值,但是当我使用 cout << *pointerToInteger; 时输出为 5 ( x=5)。

最佳答案

* 根据上下文有不同的含义。

  1. 指针声明

    int* ap;  // It defines ap to be a pointer to an int.
    
    void foo(int* p); // Declares function foo.
                      // foo expects a pointer to an int as an argument.
    
  2. 取消引用表达式中的指针。

    int i = 0;
    int* ap = &i;   // ap points to i
    *ap = 10;       // Indirectly sets the value of i to 10
    
  3. 乘法运算符。

    int i = 10*20; // Needs no explanation.
    

关于c++ - 指针中(星号)的确切用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36962658/

相关文章:

c++ - 使用已删除的指针地址

c++ - 使用 LLVM pass 添加内在函数

c++ - Coin Change 自底向上动态规划

c# - 传递数组指针以放置在 C++ 中的 HashMap 中

assembly - aarch64 是否有不可缓存(=缓存旁路)加载或存储指令?

c++ - 使用临时地址安全吗?

C++ OOP,读取文件时出现问题,EOF 使用了两次,排行榜

C函数指针行为,数组/数组指针行为

c - Shmget : Invalid argument. 为什么我得到这个错误?

c - 是否可以识别一个地址引用是否属于进程地址空间中的static/heap/stack