C + + Level Two pointers first memory model (array of pointers)

Source: Internet
Author: User


Second-level pointer the first memory model (array of pointers)
Input characteristics of pointers: allocating memory in the main function, using in the called function
Output characteristics of pointers: allocating memory within the called function, mainly by throwing out the results of the operation
Array of pointersIn the C and C + + languages, an array of array elements, all pointers, is called an array of pointers.  A one-dimensional pointer array is defined as: "type name * array identifier [array length]". For example, the definition of a one-dimensional pointer array: int *PTR_ARRAY[10]. How to understand pointer arrays A pointer array is an array of array elements that are pointers, which are essentially arrays. For example: *p[2] is an array of pointers, essentially an array, the two elements inside the pointer [] priority is higher than the priority of the *, p first with [], form the array p[2], there are two elements of the array, and then combined with the *, indicating that the array is a pointer type, each array element is equivalent to a pointer variable.UsePointer arrays can be used as parameters of functions, and are used in a similar way to normal arrays. Pointer arrays are often used to point to several strings, which makes string processing more flexible and convenient.
Example:
#include"stdio.h"#include"stdlib.h"#include"string.h"voidMain () {inti =0, j =0; Char*tmp =NULL; Char*arraystr[] = {"CCCCC","AAAA","bbbb","11111"};  for(i=0; i<4; i++) {printf ("%s \ n", Arraystr[i]); }    //Sort     for(i=0; i<4; i++)    {         for(j=i+1; j<4; J + +)        {            if(strcmp (arraystr[i],arraystr[j]) >0) {tmp=Arraystr[i]; Arraystr[i]=Arraystr[j]; ARRAYSTR[J]=tmp; }        }    }     for(i=0; i<4; i++) {printf ("%s \ n", Arraystr[i]); } System ("Pause");}

  

#include"stdio.h"#include"stdlib.h"#include"string.h"intPrintfarr (Char**ARRAYSTR,intiNum) {    inti =0;  for(i=0; i<inum; i++) {printf ("%s \ n", Arraystr[i]); }    return 0;}intSORTARRAYSTR (Char**ARRAYSTR,intiNum) {    inti =0, j =0; Char*tmp =NULL; //Sort     for(i=0; i<4; i++)    {         for(j=i+1; j<4; J + +)        {            if(strcmp (arraystr[i],arraystr[j]) >0) {tmp=Arraystr[i]; Arraystr[i]=Arraystr[j]; ARRAYSTR[J]=tmp; }        }    }    return 0;}//level two pointer the first memory modelvoidMain () {Char*arraystr[] = {"CCCCC","AAAA","bbbb","11111"}; printf ("sort before \ n"); Printfarr (Arraystr,4); Sortarraystr (Arraystr,4); printf ("after sorting \ n"); Printfarr (Arraystr,4); System ("Pause");}

One-dimensional pointer array makes function parameters degenerate to level two pointers

C + + Level Two pointers first memory model (array of pointers)

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.