Pages: [1]   Go Down
Author Topic: why DOES this code work..  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void sendString ( char *myString)
{
while (*myString> 0)
{
sendChar(*myString++);

}
}

when i send a string it displays it properly, but if i increment my string pointer from the get go it never sees index 0 which would be the first letter yet it does in fact display it ;
how so? is there a null character in beginning of my string?

the way i see is it like any array
"EDDIE"
0 1 2 3 4
E D D I E    <--see the E in the 0 position
but the very first operation in the while loop is *pointer++  so right there it goes to position one which is the first D...

im lost here smiley-sad
Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Cereal communication...
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's a post-increment, that's why. It wouldn't work if you wrote:

Code:
sendChar(*++myString);
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post increment, i++, increments after the value has been used, pre increment increment before use.

So your code reads
Code:
void sendString ( char *myString)
{
while (*myString> 0)
{
sendChar(*myString);
                *myString++;

}
}

C++ means c incremented (improved) after use  smiley

Mark
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Let's break it down:

Code:

while (*myString> 0)
{
sendChar(*myString++);

}
While the character is greater than 0:
- Print the character
- Increment the pointer

Code:
while (*myString++> 0)
{
sendChar(*myString);

}
 
While the character is greater than 0:
- Increment the pointer
- Print the character

See the subtle difference?  The ++ is performed after the test, *not* after each iteration of the while loop.  You could do it with a do...while loop though, and preincrement, as long as the first character is guaranteed NOT to be 0:
Code:
do {
    sendChar(*myString);
} while (*(++myString) > 0);
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

C++ means c incremented (improved) after use  smiley
Personally I thought it was a reference to the standardized language used in 1984 (George Orwell) - C that is double-plus good.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: