C + + Road set sail--hands

Source: Internet
Author: User

C + + first-order pointers

Defined

Store type name data type * pointer variable name;

Eg:int *a;//defines a pointer to an integral type A;

How to use pointers

int a,*b;

b=&a;//indicates that the address of a is assigned to B;

*b=5; Assign a value of 5 to the address pointed to by the B pointer, which is equivalent to a=5;

A valid value must be assigned before the pointer is used        int *a,b;         *a=5;//This expression is wrong, because a is not initialized, and assign values. void *a//Universal pointer, can accept any type of pointer,     int *a;     void *b;     char *c;  &nbsp ;     b=a;//is correct, because the void pointer can accept any type of pointer         a=b;//error because the void pointer cannot be directly assigned to other type pointers the correct wording should be a= (int *) b , convert b coercion type to shaping pointer, copy;        c= (char *) b;//correct;  second-order pointer//second-hand pointer, as the name implies, is both pointer-pointing;      & nbsp; Definition: Storage type Data type * * pointer variable name          eg  int **p;//Second-order pointer           &NBS P     #include <iostream>using namespace Std;int main () {int i,*q,**p;  i=123,q=&i,p=&q;// Simply explain the second-order pointer, Cout<<**p;return 0;} Pointer function pass value      void swap (int *x,int *y) {}int main () {int a,b,*p=&a,*q=&b;swap (P,Q); Swap (&a, &AMP;B);  pointer array with array pointer       pointer array              int *p[10];// An array of pointer types is defined, and each element in the array isA pointer type;      array pointer              int (*p) [10];//defines a pointer to an array;          #include <iostream> #include <cstdio>using namespace Std;int main () {int a[2][5]={ 1,2,3,4,5,6,7,8,9,0};int (*p) [5]=a;cout<< "*p[1]=" <<*p[1]<<endl;cout<< "* (p[1]+1) =" < <* (p[1]+1) <<endl;char *q[5]={"A", "B", "C", "D", "E"};//pointer array such initialization of Dev will have a warning, but the VC will not have any warning; it is not recommended for readers to use this initialization. cout<< "q[1]=" <<q[1]<<endl;//because Q[10] is an array of pointer types, so the address is stored in Q. return 0;}   pointer functions and function pointers       pointer function//return value is a pointer function;     Definition: type name * Function name (parameter list); Eg:int * MAX (int *x,int *y);  {      Returen *x>*y?x:y;  &nbsp ;}    Function pointers//algorithms basically not in the race, just by the way, the pointer to the function;            definition: function type (* pointer name) (function parameter list);           egint max (int x,int y) {return x>y?x:y;} int min (int x,int y) {return x<y?x:y;} int main () {int a=1,b=2;int (*p) (int,int);p =max, Cout<<p (A, B), or//can also be written as (*p) (A, B), where * operation has no meaning p=min; Cout<<p (A , b); return 0;  }  pointers and Arrays     C + + array names are both an address, so you can assign values to pointer variables;      eg          int a[ 10];*p;          p=a;//correct;        Two-dimensional array-special second-order pointer;        &N Bsp   int b[10][10];            use two-dimensional arrays as matrices;            B as second-order pointers ;              &NBSP;B refers to the line, called the row pointer;              &NB SP; Perform a * operation on B and convert to a column pointerTo determine where each element is located.              int a[10][10],**b;                  b=a;//this way is wrong.  

C + + Road set sail--pointer

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.