0

I have a while loop as below.

while (*d++ = *sc++)

As I wish to understand pointers in dept I would like to enter the while loop and understand how the while loop is working with the pointers.

I used step in gdb but it does not go into the while loop completely. Is there any way to get into the while loop and understand the manipulation in every step.

Shash
  • 4,160
  • 8
  • 43
  • 67

3 Answers3

5

* binds tighter then postfix ++. ++ on the right side will be applied last, so:

while (*d++ = *sc++)

is the same as:

while (*d = *sc)
{
  d++;
  sc++;

The modification is much better to be traced in gdb.

Update:

Don't code like this OP!

Although it might look cool, and prove you are smart. It's difficult to be parsed by the common human brain and therefore error prone? Which we do not want, do we?

Better go for a more clear alternative like proposed above and let the compiler scramble the code.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    I fully agree. Famous quote that sums this up perfectly: `"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -Brian Kernighan` – Lundin Oct 18 '12 at 09:53
  • 3
    @Lundin, Is it the same Keringham who wrote in his book "Experienced C programmers would prefer ... `while (*s++ = *t++);` ... Although this may seem cryptic at first sight, the notational convenience is considerable, and the idiom should be mastered, because you will see it frequently in C programs." (Somewhat paraphrased, "The C programming Language", Section 5.5)? – ugoren Oct 18 '12 at 10:08
  • 1
    @ugoren Indeed, he didn't have a clue about proper coding style himself, because proper coding style wasn't even invented when the K&R book was written. That book is filled to the brim with really bad coding style examples. So you have to read his statement as "Experienced C programmers who have been in coma since the mid-70s and just woke up would prefer..." – Lundin Oct 18 '12 at 10:58
  • “Epimenides the Cretan said that all Cretans were liars, and all other statements made by Cretans were certainly lies. Was this a lie?” (Bertrand Russell: Mathematical logic as based on the theory of types [http://www.cfh.ufsc.br/~dkrause/pg/cursos/selecaoartigos/Russell(1905).pdf]) ;-) @ugoren – alk Oct 18 '12 at 19:28
  • Anyway, @Lundin's Kerringham certainly understands programming better than my Kerringham. – ugoren Oct 18 '12 at 19:53
3

Option 1: Look into the assembly code debugging as suggested by Olaf Dietsche.

Option 2: Use gcc -S test.c to stop compiler after assembling to see the assembly code of your program. Understanding assembly code might be a little hard. More info here

Option 3: Rewrite your program to something like

while(1)
{
    if(*d++ != *sc++)
    {
        break;
    }
}

So that you can put breakpoints and see the values changing.

Community
  • 1
  • 1
CCoder
  • 2,305
  • 19
  • 41
0

An alternative, but identical way to write the code is:

*d = *sc;
while (*d > 0)
{
  d++;
  sc++;
  *d = *sc;
}
Shash
  • 4,160
  • 8
  • 43
  • 67
Lundin
  • 195,001
  • 40
  • 254
  • 396