In c, is there an equivalent of "+=" for "p = p->next"?

Stack Overflow Asked by Kai on December 19, 2020

I’m currently learning C and am now at the part of structs and lists.
I just wondered, if there ist something like "a = a+b" = "a += b" for this case.

2 Answers

No. You could use a macro, but i wouldn't.

Answered by 12431234123412341234123 on December 19, 2020

If you are using C++ then instead of the raw pointers you could define your own class of iterator for a list that internally contained the pointer p.

In this case you could substitute the expression

p = p->next;

for the expression


that under the hood will do p = p->next.

Something like

iterator & operator ++()
    iterator::p = iterator::p->next;
    return *this;

where the pointer p is a data member of the class iterator.

As for the compound assignment operator then there are no such operators like ->= or .=. So you can not overload them.

According to C and C++ Standards the assignment operator is one of

= *= /= %= += -= <<= >>= &= ^= |=

Answered by Vlad from Moscow on December 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP