-1

so I am a beginner pretty much but I stumbled on something confusing to me.

int* p = new int[3];
//Now I want to increment the Pointer and add value 10 there.
int* n = new int[3];


std::cout << n  << std::endl;

*n++=10; 

std::cout << n << " :: " <<  *n << std::endl; 

return 0;

However, what gets printed on the second std::cout @ * n is zero and not the expected 10. The pointer address seem to have increased by 4 bytes successfully but not the value. What am I doing wrong? Doing the same with *n+1 = 10; works like intended.

    int main()
{
    int* n = new int[3];
    
    
    std::cout << n  << std::endl;
    
    *(n+1)=10; 
    
    std::cout << n << " :: " <<  *(n+1) << std::endl; 

    return 0;
}

I also noticed that outputting following

int main()
{
    int* n = new int[3];
    
    
    std::cout << n <<"::"<< n++ ;
}

now on the n++ print, I would expect the hex value to have increased by 4, however it actually decreased by 4. Why is that? I expected it to be 4 larger than n but n++ is 4 smaller. If i do n-- the address seems to get larger? I am really confused

MP3D
  • 41
  • 5
  • 1
    Are you sure you compile with C++17? In C++17, `std::cout << n <<"::"<< n++;` should print twice the same value, whereas it is unspecified or UB before. – Jarod42 Aug 29 '21 at 08:45
  • `*n++=10;` first adds 10 to `*n`, then increments `n` (same as `*n+=10; n+=1;`). If you want the opposite order, try `*++n=10;` (Better yet, don't do such things at all, they are confusing and error-prone). – n. m. could be an AI Aug 29 '21 at 08:46
  • _@ * n_? Seriously? – Enlico Aug 29 '21 at 08:51
  • the problem in your code is the difference between `n++` and `++n`, `n++` simply first yield the **n** value, then increase it, so you are assigning 10 to `n[0]`, then `++` operator after `n` increase it, so when you print `*n` you are printing n[1] to console but you changed `n[0]`, you should use `++n` because it first increase the pointer then use it. – Ahmad Mansoori Aug 29 '21 at 09:04
  • also in last question the rule of 'n++' and '++n' is still the same, so you should the same value twice i think, because the real pointer increment happens after printing `n++` on console, so you will see the same hex value – Ahmad Mansoori Aug 29 '21 at 09:07

1 Answers1

0
*n++ = 10;

is the same as

*n = 10; n += 1;

So, in fact you are setting the first element of the array to 10, then increment n to point to the second element of the array. Then

std::cout << n << " :: " <<  *n << std::endl; 

will print address and value of the second element of the array. Since you never assigned anything to it it's value is indeterminate and technically undefined behavior occurs.


The last piece of code is more interesting:

std::cout << n <<"::"<< n++ ;

it's undefined behavior before C++17. And will print the same value twice with C++17 or later. It's all about sequence points.

See this question for a general overview and this question is almost a duplicate for it.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 1
    There is nothing to do with sequence points in `std::cout << n <<"::"<< n++;` which you provided; some details are available under the second link you provided. – Arthur P. Golubev Aug 29 '21 at 14:43