What exactly is the purpose of the (asterisk) in pointers?

前端 未结 4 1211
有刺的猬
有刺的猬 2020-12-09 14:00

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 = &         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 14:18

    * has different meaning depending on the context.

    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.
      
    2. 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
      
    3. A multiplication operator.

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

提交回复
热议问题