Pointer array and array pointer

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.