C + + Learning notes (iv)

Source: Internet
Author: User
Tags array length first string

1. Pointer (the address of the variable):
Pointer variable: The variable that holds the pointer (address)
Direct access (Access): Value by variable address
Indirect access (Access): Places the address of a variable into a pointer variable
Define pointer variable: base type * pointer variable name

2. Pointers-related operators:
&: Fetch address operator
*: pointer operator (indirect access operator);
int *pointer_1,*pointer_2;
int a = 1, b = 2;
pointer_1 = &a;
pointer_2 = &b;
Pointer_2 = &*pointer_1;//*,& priority, from right to left, so the value of a is first taken, then the address of a &a to the pointer variable pointer_2
If there is a *&a, it means to take the address of a first, then take a value.

3. Remember: Function arguments: The data between the argument-and-parameter is a one-way pass "value passing", and the pointer variable also follows this principle, when the function is called, the argument is not changed
The value of the pin variable, but you can change the value that the argument pointer variable points to. So you can use pointers to change the values of multiple variables in the central melody function without a pointer variable to achieve this
It is difficult to achieve this.

4. The pointer to the array element is the address of the array element

int a[10];
int *pointer;
pointer = a[0]; with pointer = A; The equivalent represents the address of the first element of the array.

If initial: pointer = A; The following are the prerequisites, then:
pointer+1 = a+1 = a[1]//actual address see type, pointer+1*n or A+1*n or &a[1]
so there is pointer +i = A+i = A[i] Pointer+i*n A+i*n&a[i] Example: int type n=4;

So it can be seen that [] is the variable address operator, the processing of a[i] is like this: the address of an array element is computed by a+i*n, and then the address of the cell to which it is pointing is then found to be a pointer variable that points to an array element, such as: pointer[i] and * (pointer+i) equivalent.  
So there are two ways to reference an array element:
A. Subscript method: A[i];
B. Pointer method: * (A+i) or * (pointer+i); Using this method can make the target program of high quality (small running memory, fast running)
• Note: Pointer variables can point to valid array elements, or to subsequent memory cells of the array.  Example:
int a[10], *p = A;
cout <<* (P+10);//compile without prompting for an error.

5. If you first make p=a, that is, p points to the first element of array A:
(1). p++ (or P+=1): causes P to point to the next element, that is, a[1], and if *p is used, the value of a[1] is obtained;
(2). *p++: + + with * priority, right combination, equivalent to * (p++), so is the first to take *p value, then make p++.
For example:
for (p=a;p<a+10;p++)
cout <<*p; Can be rewritten as:
for (p=a;p<a+10;)
cout <<*p++;
(3). * (p++): First fetch *p value, p plus 1
* (++p): first P plus 1, then take *p value, the difference is only the meaning of p++ and ++p.
(4). (*p) + +: First take the value of *p, then the obtained value plus 1.
(5). If P is currently pointing to A[i]:
* (p--): First take the value of *p, that is a[i], and then the pointer variable p point to a[i-1];
* (++P): First make the pointer variable from 1, point to a[i+1], and then take the value of *p a[i+1];
* (--P): First make the pointer variable from 1, point to a[i-1], and then take the value of *p a[i-1];

6. Using pointer variables as function parameters to receive array addresses
Because the array is the first address of a real parameter group when it is taken as a parameter, you can use a pointer variable to receive an argument with an array argument, for example:
void Select (int array[]);//can actually be expressed as
void Select (int *array);/* The compiler actually handles the array masterpiece as a pointer variable. So in fact, there is no one in the function call
The shape parameter group that occupies the storage space, only the pointer variable. */

7. Multidimensional Arrays and pointers:
int arr[3][4]; Suppose arr starts at 2000

(1) C + + specifies an array of the first address, so
Arr[0] is arr[0][0] 's first address, arr[1] is arr[1][0] The first address is &arr[1][0],
Arr[0]+1, the address of arr[0][1], arr[0]+2 is the address of arr[0][2];
* (arr[0]+2) represents the value of a[0][2]. So there are
Arr[i]+j means &arr[i][j],
* (ARR[I]+J) represents the value of arr[i][j].
(2) Arr, which represents the first line of address &arr[0], then a+1 represents the second line start address &arr[1], whose value is 2000+4*4=2016;
Arr[1]+1: denotes &arr[1][1], so there are
Arr[i]+j: denotes &arr[i][j],
* (ARR[I]+J): The value that represents arr[i][j], because arr[i] = * (arr+i);
* (* (arr+i) +j): The value also represents arr[i][j].
(3) pointer variable to an array element:
#include <iostream>
using namespace Std;

int main () {

int array[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
int *p;
p = array[0];//Note here is array[0], not an array, to conform to the pointer type.
cout << "--------------" <<endl;
for (p; p<array[0]+12;p++)
cout <<*p<< ";
cout <<endl;
cout << "--------------" <<endl;
return 0;
}
(4) Pointer variable points to a one-dimensional array of M elements
#include <iostream>
using namespace Std;

int main () {

int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
int (*p) [4],i,j;/* (*p) [4] is a pointer variable that points to a one-dimensional array that contains 4 integral elements, and () is not lost.
Otherwise becomes *p[4],[] high priority, becomes an array of pointers
*/
p = a;//p points to a[0];
cout << "Please input i,j:";
while (1) {
CIN >>i >>j;
if (i>=3| | J>=4)
cout << "Warning:i must less then 3,j must and then 4,please try a again:" <<endl;
else break;
}
cout << "---------------------" <<endl;
cout <<* (* (p+i) +j);
cout <<endl << "---------------------" <<endl;
return 0;
}

8. Pointers to arrays as function arguments (one-dimensional array names, multidimensional array names are also available, previously mentioned)
#include <iostream>
using namespace Std;

int main () {

void output (int (*p) [4]);
int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
Output (a);//a point to A[0]
return 0;
}

void output (int (*p) [4]) {//note parameter declared as pointer variable pointing to one-dimensional array, not int pointer variable

int i,j;
for (i=0;i<3;i++)
for (j=0;j<4;j++)
cout <<* (* (p+i) +j) << "";
cout <<endl;
}

9. Character pointers (c + + 3 ways to access a string, have learned two before)
(1) point to a string with a character pointer
#include <iostream>
using namespace Std;

Int main () {

char *str = "I love china!"; /to the character pointer variable str initialization is actually the address of the first element of the string is assigned to STR
//equivalent to char *str; str = "I love China"; When output, the system outputs the first character data that Str points to,
//Then str+1, so Point to the next character and then output ...  Until ' \ n ' is encountered, in memory, the end of the string
//is automatically added a ' cout '
<<str<<endl;
return 0;
}
(2) implements access to the string using a character pointer
#include <iostream>
using namespace std;
  
Int main () {

char str1[] = "I love Liu_xiao_min", str2[20];
Char *p1,*p2;
P1 = str1;
P2 = str2;
for (; *p1!= ';p 1++,p2++)
*p2 = *p1;//actually string str1 = "I love Liu_xiao_min", str2;str2 = str1; simplest and fastest
*p2 = ' N ';
P1 = str1;
P2 = str2;
cout <<p1 <<endl;
cout <<p2 <<endl;
return 0;
}

10. Functions and pointers
(1) Call the function with a function pointer variable (principle: A function is compiled with a portal address assigned to it.) The function entry address is called a pointer to the function)
function pointer definition:
function type (* pointer variable name) (function parameter list);
Cases:
#include <iostream>
using namespace Std;

int main () {

int max (int x, int y);
Int (*p) (int, int);//(*P) parentheses cannot be omitted
int a,b,n;
p = max;//Function name represents the entry address of the functional
CIN >>a >>b;
m = P (A, b);
cout << "max=" <<m<<endl;
return 0;
}
int max (int x, int y) {

int z;
if (x>y)
z = x;
Else
z = y;
return z;
}

(2) using pointers to functions as function parameters
Similar to: Float intern (float A, float B, float (*fun) (float));

11. Pointer function (function with function return value as pointer)
pointer function definition:
function type * Function name (parameter table column);
Cases:
#include <iostream>
using namespace Std;

int main () {
int *a (int a);
int i = 10;
cout <<a (Ten) <<endl;
return 0;
}

int *a (int a) {

Return &a;
}

12. Array of pointers (an array whose elements are all pointer-type data)

One-dimensional pointer array:
Type name * array name [array length];

Example: int *a[3];//note the difference from int (*a) [3], which is a pointer variable to a one-dimensional array containing 3 integral elements

#include <iostream>
#include <cstring>
using namespace Std;

int main () {

void sort (char *name[], int n);//pointer array do function parameters
void print (char *name[], int n);

Char *name[] = {"BASIC", "FORTRAN", "C + +", "Pascal", "COBOL"};//array of pointers
int n = 5;
Sort (name,n);
Print (name,n);
return 0;
}

void sort (char *name[], int n) {//Selection method sort

Char *temp;
int i,j,k;
for (i=0;i<n-1;i++) {
K = i;
for (j=i+1;j<n;j++) {
if (strcmp (Name[k],name[j]) >0)//name[x] The first address of each string
K = J;
}
if (k!=i) {
temp = Name[i];name[i] = name[k];name[k] = temp;
}
}
}

void print (char *name[], int n) {

int i;
for (i=0;i<n;i++)
cout <<name[i] <<endl;
}
Output:
BASIC
C++
Cobol
Fortran
Pascal

13. Pointer to pointer (pointer to pointer data)
As described in 12, the array name[x] is an array of pointers, at which point a pointer variable p, which points to the elements of the pointer array element, p is the pointer to the type of data
Pin variable.
Defines a pointer to pointer data:
char * (*P);//Can be written as Char **p;
Analysis:
Char *p: Represents a pointer variable (that is, a character pointer variable) that points to the character data.
char * (*P): Indicates that P is pointing to character pointer data
Cases:
#include <iostream>
using namespace Std;

int main () {

Char **p;//pointer variable p that defines pointer data to the character
Char *name[] = {"BASIC", "FORTRAN", "C + +", "Pascal", "COBOL"};
p = name + 2;//point to name[2]
cout <<*p <<endl;//Output NAME[2] The value of the string pointed to by the pointer
cout <<**p <<endl;//Output NAME[2] The first character in the string pointed to
return 0;
}
Output:
C++
C
Analysis:
Because *p represents name[2], it points to the string "C + +", which is the address of the first string in the string "C + +" that is stored in name[2, so "cout <<*p <<endl;" On
is the output string "C + +" starting from the first character. The **p in the second cout is the content of the first character element of "C + +" that is pointed to by *p (value name[2]), which is the character "C", so
The second cout statement outputs "C".

C + + Learning notes (iv)

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.