C and pointer summary, pointer Summary

Source: Internet
Author: User

C and pointer summary, pointer Summary

C and pointer

Basic knowledge: Memory Allocation (TAN haoqiqiang Edition)

1. What is the difference between the address of an integer variable and the address of a floating-point or complex variable? (What are the differences between integer and floating-point variables)

2. int * p indicates the pointer variable pointing to the integer data.

3. Access Integer Variables through pointer variables.

4. * p: the storage unit (variable) pointed to by the pointer Variable p)

5. p = & a --> * p = * &

6. Use pointers as function parameters

7,In the call function, because the combination of real and false values uses a one-way "value transfer" method, data can only be transmitted from the real parameter to the form parameter. Changes in the shape parameter value cannot be returned to the real parameter.

8. To reference an array element, you can use the (1) subscript method (2) pointer method (small memory usage and fast running)

9. What are the ways to improve the efficiency of pointers? (Memory, runtime ??)

10. If the pointer Variable p points to an element in the array, p + 1 points to the next element in the same array.

11. Use Pointer variables as function parameters to accept the array address. Differences between array pointers and pointer Arrays

1 # include "iostream" 2 using namespace std; 3 int main () 4 {5 void sort (int * p, int n); 6 int a [5], I; 7 cout <"input array:" <endl; 8 for (I = 0; I <5; I ++) 9 cin> a [I]; 10 cout <endl; 11 sort (a, 5); 12 cout <"the sorted array:" <endl; 13 for (I = 0; I <5; I ++) 14 cout <a [I] <""; 15 cout <endl; 16 return 0; 17} 18 19 void sort (int * p, int n) 20 {21 int I, j, k, temp; 22 for (I = 0; I <n-1; I ++) 23 {24 k = I; 25 for (j = I + 1; j <n; j ++) 26 if (* (p + j) <* (p + k) k = j; 27 temp = * (p + k); 28 * (p + k) = * (p + I); 29 * (p + I) = temp; 30} 31}View Code

12,How many bytes does an int count?Original article link

13. The combination of real parameters and form parameters has the following four forms:

Real Parameters Parameters
Array name Array name
Array name Pointer variable
Pointer variable Array name
Pointer variable Pointer variable

14. The real parameter array name a represents a fixed address, or a pointer constant, so its value cannot be changed. The form parameter array name can be changed because it is a pointer variable.

// A ++ // syntax error, which cannot be changed

15. String and pointer.String Array/string variable/character pointerStores strings.

1 // The test_3 pointer stores the string 2 # include <iostream> 3 using namespace std; 4 // # include <string> // string variable 5 int main () 6 {7 char str1 [] = "I Love C ++", str2 [20], * p1, * p2; 8 p1 = str1; 9 p2 = str2; 10 for (; * p1! = '\ 0'; p1 ++, p2 ++) 11 * p2 = * p1; 12 * p2 =' \ 0'; 13 p1 = str1; 14 p2 = str2; 15 cout <"str1 is" <p1 <endl; 16 cout <"str2 is" <p2 <endl; 17 return 0; 18}View Code

16. Functions and pointers. The function entry address is called a function pointer.

1) Use a function pointer to call a function.

Function Type (* variable name) (function parameter table)

1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 int max (int x, int y); 7 int (* p) (int, int); 8 int a, B, m; 9 p = max; 10 cin> a> B; 11 m = p (a, B ); 12 13 cout <"max is" <m <endl; 14 return 0; 15} 16 17 int max (int x, int y) 18 {19 int z; 20 if (x <y) 21 z = y; 22 else23 z = x; 24 return (z); 25}View Code

2) use pointers to functions as function parameters.

3) function that returns the pointer value: pointer function.

Type name * function name (parameter list); int * a (int x, int y );

17,

1) pointer to a constant (the value of the object to which it points cannot be modified through the pointer variable)

Const type name * pointer variable name

2). Regular pointer (the value of the specified pointer variable is a constant, that is, the pointer variable cannot be changed)

Type name * const pointer variable name

Tips: It must be defined as initialization and specified to point.

The pointer variable points cannot be changed, but the pointer variable points to the variable value can be changed. * P2 = 12; // valid

Note (position of const and ). Const is behind *. Compare it with the form of pointer variable that defines the variable pointing to the constant variable.

3). A constant pointer to a constant (the pointer variable points to a fixed object and the value of this object cannot be changed ).

Const basic type name * const pointer variable name

18. void pointer type

19. References and pointers

Why use references?

It is mainly used as a function parameter to expand the function's ability to transmit data.

(1) Use the variable name as the real parameter and form parameter. Value transfer is one-way, and real parameters are passed to the form parameter. Changes in the form parameter do not affect the value of the real parameter.

Because when calling a function, the form parameter and the real parameter are not in the same storage unit.

(2) Use Pointer variables as the form parameters to swap the values of two variables.

(3) take reference as the form parameter, and create a reference to the variable when the actual and actual conditions are combined, so that the form parameter name is used as the reference of the real parameter, that is, the form parameter becomes the reference of the real parameter.

Using namespace std; int main () {// const int a = 100, B = 10; void swap (int * p1, int * p2 ); // void swap (int & p1, int & p2); int a = 10, B = 100; int * pointer_1, * pointer_2, * p; pointer_1 = &; pointer_2 = & B; cout <a <"" <B <endl; swap (pointer_1, pointer_2); // swap (* pointer_1, * pointer_2 ); // * pointer_1 = 20; // if (a <B) // {// p = pointer_2; // pointer_2 = pointer_1; // pointer_1 = p; //} cout <a <"" <B <endl; cout <* pointer_1 <"" <* pointer_2 <endl; return 0 ;} void swap (int * p1, int * p2) // void swap (int & p1, int & p2) {int temp; temp = * p1; * p1 = * p2; * p2 = temp; // int temp; // temp = p1; // p1 = p2; // p2 = temp ;}View Code

Related Article

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.