2

Looking at this code from The Evils of Arduino Strings

void PrintString(const char *str) {
    const char *p;
    p = str;
    while (*p) {
        Serial.print(*p);
        p++;
    }
}

I was looking at reducing/compacting it. Firstly, this seems to be equivalent:

void PrintString(const char *str) {
    const char *p;
    p = str;
    while (*p)
        Serial.print(*p++);
}

Now, looking at the first two lines, how can I combine the following two lines, is it possible?

    const char *p;
    p = str;

Can I do this?

    const char *p = str;

This would seem likely but looks unbalanced, in that there is a lack of an asterisk on the right hand side. I tried it and it seems to work but I was wondering whether it was correct, and worried that I would end up with some hard-to-track-down run-time error later on, further down the line.

However, this line below is clearly wrong (as it would change the pointer p to point to a location given by the value of the first character of the C string str):

    const char *p = *str;
10
  • 2
    p and str have the same type. Would you have the same doubts if the type were spelled "pointer_to_const_char" instead of "const char *"?
    – molbdnilo
    Oct 16, 2018 at 10:52
  • 3
    If you want compact: while (*str) Serial.print(*str++);.
    – molbdnilo
    Oct 16, 2018 at 10:53
  • 2
    @Mayur No actually you were right - I thought this was printing string after string, but that would be a ** input. (After all it looks like Serial.Print can just print a string in one go from the documentation.) This is printing character by character so your initial version is fine.
    – Rup
    Oct 16, 2018 at 10:59
  • 2
    const char *p = str; ...*looks unbalanced*. You're not after a balanced look but correctness of type. p is a char *. So is str. So it is fine.
    – lurker
    Oct 16, 2018 at 11:03
  • 2
    @Greenonline Yes, you don't need a local variable - that could be the entire body. (But I would break it into two lines for readability.)
    – molbdnilo
    Oct 16, 2018 at 11:03

4 Answers 4

6

This is obfuscation. The original code is perfectly clear, there is no need to change anything. Your changes make it less readable.

What you can do to increase readability slightly, and this is a cosmetic change, is to use a for loop instead:

void PrintString (const char *str) 
{
    for(const char* p=str; *p != '\0'; p++)
    {
        Serial.print(*p);
    }
}

Alternatively:

void PrintString (const char *str) 
{
    for(size_t i=0; str[i] != '\0'; i++)
    {
        Serial.print(str[i]);
    }
}
1
  • @alk The question used an idea which doesn't make sense.
    – Lundin
    Oct 18, 2018 at 7:05
2
const char *p = str;

This would seem likely but looks unbalanced, in that there is a lack of an asterisk on the right hand side.

If you separated the elements of the above snippet correctly it would not look "unbalanced" at all:

  • const char* is the type (a pointer to const characters)
  • p and str are the variables, both pointer (to const char)

You could write the above using some unconventional layout like this:

const char *  /* Note the lack of a semicolon */
p = str;

The above code ends up with p being defined as a pointer to char and carrying the value of str.

You get same "result" for the below code

const char *p;
p = str;

Note that the former is called "initialisation", whereas the latter is called "assignment".

1

You can omit the use of the p pointer, use directly the str pointer argument, since it is a copy of the pointer to the constant content.

The changes made on str pointer don't affect the pointer used when calling the function, since the pointer is passed by value.

void PrintString(const char *str) {
    while (*str) Serial.print(*str++);
}
1

How about replacing the while with for. Here's my take:

void PrintString(const char *str)
{
    for(const char* p = str; *p; p++)
        Serial.print(*p);
}

I think this could work, too:

void PrintString(const char *str) {
    for(const char* p = str; *p; Serial.print(*(++p)))
        ;
}

As others have pointed out, we can replace p with str, since str is a local copy, just like any other variable passed as function argument.

void PrintString(const char* str) {
    for( ; *str; Serial.print(*(++str)))
        ;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.