This code calls a function to send a text string to an LCD display:
lcdputs( "LCD test " );
other code
void lcdputs(const char *s)
{
while( *s )
lcdwrite( *s++ );
}
Am I right to interpret this as:
1. pass the string "LCD test " to the function lcdputs.
Yes. Well, to be precise, not the "string" as an object, but a pointer to it.
2. lcdputs receives not the string but the address of the string and represents this as *s.
As per 1. lcdputs() receives as its only argument a pointer to the string. 'const char *' represents a pointer to a char. Could be a single char, or a consecutive buffer of several chars.
The 'const' qualifier has two purposes: gives an hint to the developer that the function WILL NOT modify the "string" that is pointed to (so you can consider this as a "read-only" qualifier here), and effectively prevents modifying the content of the string inside the lcdputs() function. If you attempt to write code that does, the compiler will complain.
3. while the character contained in the address *s is not zero ...
Yes. Although I prefer saying "pointer" than "address". Whereas pointers in C are often effectively addresses, they could be anything really. Just call them pointers.
'*s' represents a dereference, thus the object pointed to by the pointer 's'. (Which is effectively the corresponding char here.)
4. pass the character contained in the address *s to the function lcdwrite
The address (again I prefer pointer) is not '*s', it's 's'. You really need to get that if you want to understand pointers in C. '*s' is not a pointer, it's a dereference of a pointer.
What may confuse you (or beginners in general) is that declarations of pointers use the '*' symbol (to indicate they are pointers), but in any expression using pointers, '*' is the dereference operator.
Two different things.
5. and after returning... increment the address pointer (correct term?) by one to get the next character in the string.
Not really. The pointer's post-incrementation here is guaranteed in C to be executed AFTER the dereference (so '*s++' effectively corresponds to the same char as '*s'), but it's not guaranteed to be incremented AFTER the function call here (as it's passed as a parameter). Not that it really matters here, but do not expect expressions in function arguments to be evaluated in any particular order.
6. the final character is a space which triggers the while loop to end somehow.
No, here the only character that will end the loop is 0. 0 is not a space. It's just conventionally the marker of the end of a - you guessed it - zero-terminated string (which is the convention with C strings.)
And there is no "somehow".
The loop will end upon a nul (0) character because 0 as a condition in C is considered false. That's all there is to it.
To make the intent a bit more clear, I tend to prefer writing this as: while (*s != 0). But it's strictly equivalent in C.