Pointer Learning (small blackboard) and pointer blackboard in C Language

Source: Internet
Author: User

Pointer Learning (small blackboard) and pointer blackboard in C Language

 

Pointers are the essence of the C language and the danger of the C language. Today we review the C language and make a summary.

You are welcome to grade.

 

 

(1) pointer meaning
A pointer is also a data type. It is a variable pointing to an address.
For example:

{Int a = 10; int * B = & a; // obtain the address of}

Memory display

Stored value of address storage variable
F1 ----> a (integer data) <--- 10
F2 ----> B (pointer type) <--- address of

So it should be noted that the initial value should be assigned to pointer-type data, otherwise it will be dangerous.

If only pointer variables are declared and not operated, the value itself is the value left by the last program in the storage area,
At this moment, it is regarded as an address. If you assign a value to the variable pointed to by this address, it may modify the system's internal things,
This is the risk of pointer operations in C language.
For example:

{  int *a;  *a = 10;}

In Memory

F1 ----> a (pointer type) <--- memory value (as address data)

If the memory value is 00, it points to the part starting from the system program.

Then, if you assign the value of 00 to 10, an error occurs.

Note: int * p, q; declares the pointer P to declare the int type variable q.

(2) pointer Application
Pointers are commonly used in two types of applications.
1> when a function returns multiple values, a simple return statement cannot solve the problem. Instead, a pointer is required for value transfer.
The parameter passed by the function is the value of the result to be saved.

swap(int *a,int *b){    int c = 0;    c = *a;    *a = *b;    *b = *a;}

2> when the function needs to return the status (that is, whether the function operation is successful), you need to use a pointer to pass the result and return the operation status with return.

Example: Division Function

void chufa(int a,int b,int *result){    if(b == 0)    {         return 0;    }    else    {        *result = a/b;         return 1;    }}

(3) Meaning of array variable parameters

In the following example, if an array is declared, a actually represents the starting address of array a, that is, the address of a [0 ].
Then a becomes a pointer variable parameter.

{Int ar [5]; int P = & ar [0]; // int P = ar // The meanings of the following four types are the same, the first parameter is the first address of the array: int sum (int ar [], int n); int sum (int [], int); int sum (int * ar, int n); int sum (int *, int );}

Display in memory

F1 ---- ar [0]
F2 ---- ar [1]
F3 ---- ar [2]
F6 ---- * P <---- F1 (address of ar [0)
Then p + 1 <---- F2 P + 2 <---- F3
* (P + 1) = ar [1]
Note: array ar is a constant pointer, that is, const cannot be assigned or changed!

(4) Relationship between const and pointer
Two Forms
1>
Int * const q = & I; // q is a const pointer.
Q cannot be changed. * q can be assigned, but q cannot be changed.
2>
Const int * p1 = & I; // indicates that I cannot be modified through * q
The distinction between the following situations depends on the positional relationship between the const and *. If the const is behind *, the pointer cannot be modified.
Otherwise, the value cannot be assigned through * p.
Const int * p1 = & I;
Int const * p2 = & I;
Int * const p3 = & I;

Application: In a function, const is often added before an array parameter to protect the array from being damaged.

(5) pointer operation
1> Add one minus one
Int * a = & I;
A ++; // Add an int length to the address
A --; // subtract an int length from the address
In Memory
0xffffff64 ----> I
Then
A = 0xffffff64
After the ++ operation, a becomes 0xffffff68
A -- after calculation, a changes to 0xffffff60.

2> Subtraction
The offset between two pointer variables of the same type is the data width of the int whose difference is multiple.

3> * P ++ operations
Assign a value first and then add one.

4> comparison
Compare size> <=! =

5> void * p pointer
Indicates that the pointer points to an area of unknown size.


// Malloc dynamic memory allocation

# Include <stdlib. h> int main () {int number; int * a; printf ("Enter quantity"); scanf ("% d", & number); a = (int *) malloc (number * sizeof (int); // dynamic memory request for (int I = 0; I <number; I ++) {scanf ("% d ", a [I]);}/* use */free (a); // release the memory return 0 ;}

(6) Introduction to pointers and 2D arrays

Char a [10] [10]; // declare a two-dimensional array
Char ** P = & a [10] [10];

The first address of the pointer variable array forms an array Area a [] []
P -------> a [0] --------> [array of the first line] // a [0] is the first address of the first line of the Two-dimensional array
P + 1 -------> a [1] --------> [array of the second line] // a [1] is the first address of the second line of the Two-dimensional array
P + 2 -------> a [2]


So
P is the address of a [0], and a [0] is the first address of the first line of data.
Therefore, P is the pointer of the pointer.
Example:
* P is a [0]
** P is a [0] [0]
* (* P + 1) is a [0] [1]

 

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.