A pointer is a variable that holds the address of another variable. In C language, pointers are widely used. Sometimes it's because it's not available, sometimes because it makes the code more compact and more efficient. The pointer is also a time bomb, a slight carelessness will cause the program to crash. In addition, the pointer flying can also affect the readability of the program. Pointers and arrays are closely related, and can be achieved by using pointers, where the array is basically used.
Recently engaged in GPS and SMS parsing procedures, it mainly involves the parsing of strings, such as the resolution of GPRMC statements of GPs, the parsing of custom SMS control instructions, which can be solved with two-dimensional character array, but the effect is much worse than the array of pointers, the execution efficiency is low and memory usage is large. A bit of hardship or flattery. The following is a simple comparison of the two methods of implementation.
TCHAR szgpssentence[128]; GPRMC statement to be resolved
TCHAR szgpsfields[16][16]; BUF for each field when using a two-dimensional array
TCHAR *pgpsfields[16]; A pointer to a field when using an array of pointers
Parsing code when using a two-dimensional array
int SplitSentenceToFields1 (TCHAR *src,tchar szfields[16][16])
int SplitSentenceToFields1 (TCHAR *src,tchar szfields[][16])
int SplitSentenceToFields1 (TCHAR *src,tchar (*szfields) [16])
{
int i = 0;
int j = 0;
while (*SRC)
{
if (*src!= _t ("))
{
Szfields[i][j++] = *SRC;
}
Else
{
SZFIELDS[I++][J] = 0;
j = 0;
}
src++;
}
SZFIELDS[I++][J] = 0;
return i;
}
Parsing code when using an array of pointers
int SplitSentenceToFields2 (TCHAR *src,tchar *pfields[])
{
int i = 0;
if (*SRC)
{
* (Pfields + i++) = src;
while (*SRC)
{
if (*src++ = = _t ('))
{
* (Pfields + i++) = src;
* (src-1) = 0;
}
}
}
return i;
}