mikalhart / TinyGPSPlus

A new, customizable Arduino NMEA parsing library
http://arduiniana.org
1.05k stars 486 forks source link

Can anyone help me clear my mind #97

Open WXQ6989 opened 2 years ago

WXQ6989 commented 2 years ago

I am new in C++ here, In all this code, how to divide and parse the required time or location information?

and how to understand the following sentence:

for (TinyGPSCustom *p = customCandidates; p != NULL && strcmp(p->sentenceName, customCandidates->sentenceName) == 0; p = p->next) p->commit();

Thank you very much

TD-er commented 2 years ago
for (
      TinyGPSCustom *p = customCandidates;  // 1
      p != NULL && strcmp(p->sentenceName, customCandidates->sentenceName) == 0; // 2
      p = p->next) { // 3
  p->commit();
}

I reformatted your piece of code a bit and added the comments 1, 2, and 3.

In a for loop, you have 3 parts, separated by a semicolon (;)

  1. Declaration
  2. Check on every loop, whether to continue of not.
  3. Code to execute after a loop, before the next one.

Typical for loop:

for (int i = 0; i < 10; ++i) {

}

Back to your code:

  1. customCandidates is a list of pointers, so you get the first element of it.
  2. Check if the pointer is not null and if so, compare sentenceName of the element p with the first element, to see if it is the same.
  3. Get the next elemeent

In short, it looks like this checks the list of candidates to see if the strings match and calls commit() on those. N.B. this for loop stops as soon as a string is found that does not match, so the order is important.