Incrementing pointer not working

前端 未结 5 1893
后悔当初
后悔当初 2021-01-24 02:30

I’m trying to increment pointer. I was sure that it is similar to do i+=1 , but I’m getting adress.

#include \"stdafx.h\" 
#include          


        
5条回答
  •  不知归路
    2021-01-24 02:50

    If you want your output to be "43", than you have to change *a++; to (*a)++;.

    But other than for testing and learning, code like yours is more of a "C thing" than a "C++ thing". C++ offers another approach to referencing data and operating on it, through what the language calls “references”.

    int i=42;
    int &a=i;  // ‘a’ is a reference to ‘i’
    a++;
    assert(i==43);  // Assertion will not fail
    

    References are especially useful for passing arguments to functions, without the risk of having null or displaced pointers.

提交回复
热议问题