Pointer & pointer to pointer array

Source: Internet
Author: User

A pointer to a pointer
    The pointer's pointer looks somewhat confusing. Their declarations have a two asterisk. For example:
Char * * CP;
If there are three asterisks, that is a pointer to the pointer pointer, four asterisks is a pointer to the pointer pointer, and so on. When you are familiar with simple examples, you can deal with complex situations. Of course, in the actual program, it usually only uses a level two pointer, three asterisks are uncommon, let alone four asterisks.
    The pointer's pointer needs to use the pointer's address.
Char c= ' A ';
Char *p=&c;
Char **cp=&p;
A pointer to a pointer can access not only the pointer it points to, but also the data it points to. Here are a few examples:
Char *P1=*CP;
Char C1=**CP;
You may wonder what the use of such a structure is. Pointers can be used to allow the called function to modify local pointer variables and manipulate array of pointers.

void Findcredit (int * *);

Main ()
{
int vals[]={7,6,5,-4,3,2,1,0};
int *fp=vals;
Findcredit (&FP);
printf (%D\N,*FP);
}

void Findcredit (int * * FPP)
{
while (**fpp!=0)
if (**fpp<0) break;
Else (*FPP) + +;
}

first, the pointer FP is initialized with the address of an array , and the address of the pointer is passed as an argument to the function Findcredit (). the Findcredit () function indirectly obtains the data in the array through an expression **FPP. To iterate through the array to find a negative value, the object that theFindcredit () function is self-increment is the caller's pointer to the array, not its own pointer to the caller's pointer. the statement (*FPP) + + is the self-increment of the pointer to which the parameter pointer is pointing. However, because the * operator is higher than the + + operator, the parentheses are required here, and if there are no parentheses, the + + operator will be used on the double pointer FPP.

pointer to pointer array
    Another use of pointer pointers is to handle arrays of pointers. Some programmers prefer to use pointer arrays instead of multidimensional arrays, and a common use is to manipulate strings.

Char *names[]=
{
Bill,
Sam,
Jim,
Paul,
Charles,
0
};

Main ()
{
Char **nm=names;
while (*nm!=0) printf (%s\n,*nm++);
}

first , the pointer nm is initialized with the address of the names array of character pointers . Each call to printf () first passes the character pointer pointed to by the pointer nm, and then the nm is self-increment to point to the next element (or pointer) of the array . Note that the syntax described above is *nm++, which first obtains what the pointer points to and then makes the pointer increment.
Note that the last element in the array is initialized to 0, and the whileLoop Determines whether the end of the array is reached. pointers with a value of 0 are often used as terminators for loop arrays. The programmer calls the 0-value pointer a null pointer (NULL). Using a null pointer as the terminator, you do not have to change the code that iterates through the array when the array is deleted or not, because the array still ends with a null pointer.

Pointer & pointer to pointer array

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.