Author Topic: Pointer arithmetic - casting pointer to other type  (Read 1759 times)

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Pointer arithmetic - casting pointer to other type
« on: May 22, 2023, 08:23:03 pm »
I'm new to C++ and this language seems to be a little bit vague for me.

Please explain, for what reason I should use such code
Code
void* Src;
{
  t* TempSrc = (t*)Src;
  ...
  TempSrc++;
  ...
  Src = (void*)TempSrc;
};
instead of just
Code
  void* Src;
  (t*)Src++;
?

In second case compiler complains about incrementing void pointer, so I assume, that pointer casting doesn't work. And in case of ((t*)Src)++ compiler complains about lvalue.
« Last Edit: May 22, 2023, 08:33:04 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1560
Re: Pointer arithmetic - casting pointer to other type
« Reply #1 on: May 22, 2023, 08:42:22 pm »
Programming questions are OT here.

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Pointer arithmetic - casting pointer to other type
« Reply #2 on: May 22, 2023, 09:12:18 pm »
Ok, seems to be fixed via following trick:
Code
Src = (void*)((t*)Src + 1);
Is it healthy for project not to have regular stable releases?