Mad Son c_c++ notes

Source: Internet
Author: User
Tags define null

Pointer Chapter

1. Basic pointer variables

(1) Definition

int i,j;

int *pointer_1,*pointer_2;

pointer_1 = &i;

pointer_2 = &j;

Equivalent to

int *pointer_1 = &i,*pointer_2 = &j;

(Pointer misunderstanding: we should first know that the pointer is an address, is immutable; pointer variables (the pointer_1 defined above are pointer variables) are variables, variables are mutable, and the amount of CPU that is stored by the usual variable is treated as the address.


(2) Précis-writers

*: Take the address space storage, * after the contents of the CPU when address processing;

&: Take the address of the variable,& after the contents of the CPU as a variable to deal with;

2. Pointer variables for one-dimensional arrays

(1) Definition

int a[10];

int *p;

p = &a[0];

Equivalent to P = A;

A[0] and a both represent the first address of the array;

(2) pointer variable to an array can also be subscript, such as: P[i], * (p+i);

(3) The array name a represents the address of the first element of the array, it is a pointer constant, its value is fixed during the program run, please do not appear similar to a++ error.

3. When the function parameter is passed to the function by the array name, it is the first element address of the array, so the parameter is required to be a pointer variable;


①: Parameters and arguments are used as array names

int a[10];

f (a,10);




Viod (int x[], n)


Simple solution: The compiler encounters a parameter array name as a pointer variable to handle, so the program runs during the parameter to the array a[] elements, or to understand that the parameter arguments in the program run interval occupy the same memory space;

② Real parameter group name, parameter pointer variable

int a[10];

f (a,10);




Viod (int *x, n)


Simple solution: The function begins execution x points to each element of the array

③ argument parameters are pointer variables

int a[10], *p = A;

F (P, 10);




void f (int *x,int N)


Introduction: 1. P first points to array a first element; 2. P-value to x; 3. x points to each element of array A;

④ argument is pointer variable, parameter is array name

int a[10],*p = A;

F (P, 10)

void f (int x[],int N)


Simple solution: 1. Parameter when the pointer variable is processed, followed by the same ③

4. Properties of Array A

int A[3][4] = {{1,3,5,7},{9,11,13,15},{17,19,21,23}}

int data in the Keil compilation environment, accounting for 2 bytes of memory





Two-dimensional array name, pointing to an array a[0], which is the 0 header address

Set 2000

A[0], * (a+0), *a

0 row 0 Column element address



1 row 0 Column element address


A[1], * (a+1)

Address of 1 Rows 0 column element a[1][0]


A[1]+2, * (a+1) + 2, &[1][2]

1 row 2 column (i.e. a[1][2]) element address


* (a[1]+2), * (* (a+1) +2), a[1][2]

1 rows 2 columns (i.e. a[1][2]) element values

Element 13

(1) Why does * (a+1) indicate the first address of a line?

Answer: * (a+x) ==a[x]; The two are equivalent.

(2) How does the C language cause * (A+X) and a[x] to be completely equivalent?

A: In an array, *a is a[0],a+1 pointing a[1],a+2 to a[2],a+3 pointing to a[3], which means

* (a+1), * (a+2), * (A+3) respectively a[1], a[2], a[3]. In the actual code generation mechanical code relationship, two effects are completely equivalent.

5. Interpretation of Int (*P) [4]

A: Int (*p) [4] means p is a pointer variable that points to a one-dimensional array containing 4 shaping elements, where p can only point to a one-dimensional array containing 4 elements, p cannot point to an element in a one-dimensional array, and the value of P is the starting address of the one-dimensional array.


(*P) [0]

(*p) [1]

(*P) [2]

(*P) [3]

This can also be derived

Pointers as function arguments

① pointer variable to variable (general)

② pointer variable pointing to a one-dimensional array

6.while (*from! = ') "= = while (*from! = 0), when the CPU is processing, the characters are processed in the same way as the ASCLL code.

7. Character array and character pointer variables

Char str[14];

str = "I love China"; X (the character array can only be assigned to individual elements)

An array can only be assigned the initial value at the time of definition, but it cannot be assigned to the whole of the value statement;

But (aber)

Char *a;

A = "I love China"; √ (note that assigning to a is not a character, but the address of the first element of the string)

Equivalent to

Char *a = "I love China";

/****** Code printf******/

Char *format;

format = "a=%d,b=%f\n"; When string processing

printf (format,a,b);

Equivalent to

printf ("a=%d,b=%f\n", A, b);

8. Pointers to functions

#include <stdio.h>

void Main ()


int Max (int,int);

Int (*p) (int,int);

int a,b,c;

  p = max;

scanf ("%d,%d", &a,&b);

c = (*p) (A, b);

printf ("a =%d,b =%d,max =%d\n", a,b,c);


(1), "Int (*p) (int,int);" To define p as a pointer variable to a function, the brackets at both ends of the *p must never be omitted.

(2), the functionof "p = max" is to assign the entry address of the function max to the pointer variable p. Array names are similar to the first element addresses of arrays, and function names represent the entry addresses of the functions . (similarly: max++ is wrong)

(3), a general definition of a pointer variable that points to a function:

  Data type (* pointer variable name) (function parameter table column);

9. use pointers to functions as function arguments

Such as:

int a = 1;

int b = 2;

int max (int x,int y)


void Process (Int,int,int (*fun) (Int,int)); Statement

Process (A,b,max); Reference

10. Functions that return pointer values

Type name * Function name (parameter list);

Such as:

int *a (int x, int y);

Explanation: The operation priority of parentheses is greater than the * number, and is the return value of function a (int x, int y) take address value;

Please distinguish the Int (*p) (int,int) in question 8, which is a pointer to the function;

Int (*p) (int,int); Pointers to functions

int *a (int,int); Functions that return pointer values

The two use the opposite order


float *p;

Float Score[][4] = {{1,2,3,4},{5,6,7,8},{11,12,13,14}};

Float *search (Float (*pointer) [4],int N);

p = Search (score, m);

11. Array of pointers

(1) Definition form

Type name * array name [array length];

such as: int *p[4];

Please do not write int (*p) [4]; This is a pointer variable pointing to a one-dimensional array;

Each amount in the pointer array is used as a pointer variable;

Why use an array of pointers?

A: It can be more appropriate to point to a number of strings, making string processing more convenient and flexible.

12. Pointers pointing to pointers

(1) Definition

Char **p;

Remember: *p is the address of **p, p is the address of **p, and **p is a variable. Both P and *p are addresses;

There are two * numbers in front of p, and the binding of the * operator is right to left, so **p is equivalent to * (*P), and obviously *p is the defining form of the pointer variable.


#define NULL 0 (the sentence is already in stdio.h)

p = NULL;

Indicates that P does not point to any of the useful units, it should be noted that the value of P is null and the non-p assignment is two different concepts. The former is a value (0), does not point to any program variable, the latter does not assign a value to p but not equal to P no value, but its value is an unpredictable value, that is, p may point to a unit that has not been specified beforehand. This is a dangerous situation. Therefore, you should assign a value to a pointer variable before referencing it.

Any pointer variable or address can be compared to null as equal or unequal, for example:

if (p = = NULL) ...

14. Pointer operations to the same array

(1) Subtraction, Comparison


P1 point A[1],p2 point to a[3]


Subtraction: P2-p1 = 2, but P1 + P2 is meaningless;

Comparison: pointer variable "less than" pointing to the preceding element to a pointer variable that points to a subsequent element.

That is: P1 < P2, or "P1 < P2" is 1 (true);

15.void pointer type

Char *p1;

void *p2;




P1 = (char *) P2;

You can also:

P2 = (void *) P1;

Void is used to point to an abstract type data, assigning its value to another pointer variable when it is forced to make the type conversion appropriate to the type of the variable being assigned.

15. Pointer Type Summary

Defined Meaning
int i; Defining integer Variables I
Int*p; P is a pointer variable that points to an integral type of data
int a[n]; Defines an integer array A, which has n elements
int *p[n]; Defines the pointer array p, which has n pointer elements (many) that point to an integer data
int (*p) [n]; P is a pointer variable (one) to a one-dimensional array with n elements
int f (); F is a function that returns to the value of an integer function
Int*p (); P is a function that returns a pointer to an integral type of data
Int (*p) (); P is a pointer to a function that returns an integer value
int **p; P is a pointer variable that points to a pointer variable that points to an integral type of data

Next Update Time 2014.09.26

New understandings and insights can be exchanged with each other and learn together

Mad Son c_c++ notes

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: 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.