指针中(星号)的用途到底是什么? [英] What exactly is the purpose of the (asterisk) in pointers?

查看:549
本文介绍了指针中(星号)的用途到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,我正在尝试将指针"的想法包扎起来.

I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.

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

}

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

Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).

推荐答案

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

* has different meaning depending on the context.

  1. 指针的声明

  1. Declaration of a pointer

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.

  • 取消引用表达式中的指针.

  • Dereference a pointer in an expression.

    int i = 0;
    int* ap = &i;   // ap points to i
    *ap = 10;       // Indirectly sets the value of i to 10
    

  • 乘法运算符.

  • A multiplication operator.

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

  • 这篇关于指针中(星号)的用途到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆