For construct with pointers [SOLVED]

Hi,

In Nick Gammon's forum, I read about SPI. There is a for( ; ; ) construct that uses pointers.

for (const char * p = "Fab" ; c = *p; p++)
    SPI.transfer (c);

for (const char * p = "Hello, world!\n" ; c = *p; p++)
  SPI.transfer (c);

The initialisation part sets a pointer to a string (no null termination).

The continue for clause part: c = *p is the part that I don't understand

The last part just advances the pointer as expected.

Can anyone shed some light on this for me?

Thanks all.

Jacques

All hardcoded strings include the null at the end. That's why char strConst[]="abc"; has 4 characters in memory. The value of the null is zero. What is the internal representation of FALSE?

Remember an assignment (single =) is an expression which returns a value. The value which is assigned.

a = b = c; will give both a and b the value of c.

:wink:

The initialization does have null termination, it's a string in quotes!

c=*p sets c to the current value pointed to by p - and that value is interpreted as true or false for purposes of whether the loop keeps going. As long as c is not 0, that's true and the loop keeps going. When it hits the null terminator, c gets set to 0, which is false, so the loop ends at that point.

I forgot about the null character being automatically appended to hard coded strings. I must have read that line in the reference page half a dozen times...

Thanks Morgan.

EDIT: And you too DrAzzy