Dark Horse programmer--c The language of pointers-my notes

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

-------------------------------Pointer---------------------------------

Pointer?

The number of the memory unit is also called the address. Since the desired memory unit can be found based on the number or address of the memory unit, the address is often referred to as a pointer.

---Summary: For a memory unit, the address of the cell is a pointer, where the data stored is the contents of the unit.

Benefits of using pointers

A. Provides a flexible means of modifying the calling variable for a function;

B. Having multiple return values for a function

C. The efficiency of certain subroutines can be improved

>> when data is passed, if the chunk is large (such as a data buffer or a larger structure), you can use the pointer to pass the address instead of the actual data, which increases the transfer speed and saves a lot of memory.

D. Providing support for dynamic data structures such as binary trees, linked lists

3. There are two ways to access the variable: direct access and indirect access

int a = 3; Direct access: Assignment and value of variables (access value via variable name) int b = a;printf ("%d", b);

Indirect access: Through the pointer (address) indirect operation is completed.

int *b = &a;printf ("%d", *b);

-----------------------------pointer variable

1, pointer variable in the C language, allows a variable to hold the pointer, which is called the pointer variable.

Therefore, the value of a pointer variable is the address of a memory cell or a pointer to a memory unit.

Note: In strict sense, a pointer is an address that is a constant pointer variable that holds an address and is a variable.

Define a pointer variable

The definition of a pointer variable consists of three contents:

1) Pointer type description, that is, define the variable as a pointer variable;

2) pointer variable name;

3) Variable value (pointer)

Its general form is: type descriptor * variable name;

----NOTE:

1) When the pointer is defined, the "*" sign indicates that the defined variable is a pointer variable,

The value of a variable can only hold an address.

2) A pointer of one type can only point to a variable of the same type and cannot point to another type of variable.

3) Pointers can also be declared as global, static local, and local.

-----initialization and referencing

There are two ways to initialize a pointer variable:

Defined and initialized at the same time as the first defined

1) initialization at the same time as defined

int a = 5; int *p = &a;

2) First define the post-initialization

int A; int *p; p=&a;

3) Initialize the pointer to NULL int *p=null;

int *q=0; Illegal initialization:

1) The pointer variable cannot be assigned an integer value (because we do not know which block of memory this shaping constant is

2) The assigned pointer variable can no longer be added "*" specifier, such as written as *p=&a is also wrong

---be careful.

1, multiple pointer variables can point to the same address int a = 5;

2, the pointer can change the direction of

3, the pointer is not initialized inside is a garbage value, this time we this is a wild pointer, if the operation of a wild pointer

1) may cause a program to crash

2) Access you should not access the data so the pointer must be initialized before it can access the storage area it points to

Two related operators:

&: Take address operator;

*: pointer operator (or "indirect access" operator).

& variable name;

* pointer variable name//Gets the contents of the storage space that the pointer variable points to, such as &a represents the address of the variable A, &b represents the address of the variable B. The variable itself must be described in advance.

-----about the use of * Note:

1) when defining a variable * is a type specifier, stating that the variable defined is a pointer variable

int a = 5; int *p = &a;int *P1 = p;

2) When a variable is not defined * is an operator that accesses the storage space pointed to by the pointer

int x = *p;

--------function for two-variable exchange

Analysis:

General practice cannot realize the exchange of variable values.

No pointer address Exchange can be implemented

The value exchange pointed to by the pointer can be implemented

void swapNum2 (int *a,int *b) {    int temp;    temp = *a;     *a = *b;     *b = temp;}

The keynote function must pass the address of the same data type

Common application scenarios for------------pointers

1) You can modify the values of variables in the keynote function in the modulated function

void Change (int*num) {  *num = 10;}

2) function returns multiple values

void Sumandminusandjiandshang (int num1,int num2,int *sum,int *minus,int *ji,int *shang) {    *sum = num1 + num2;    *minus = num1-num2;    *ji = Num1 * NUM2;    *shang = num1/num2;}

-------------Level Two pointers

If a pointer variable holds the address of another pointer variable, the pointer variable is called a pointer variable pointing to the pointer (address). Also known as the "level two pointer"

int a = 5;

int * p = &a;

int * * P1 = &p;

P---address to a

*p---A's address corresponds to a value----that is 5.

p1--the address of the pointer variable p

*P1---> P address---A

* *P1----> * A's address--*p-5

Why do------------pointers differentiate between types

A pointer variable occupies a fixed amount of memory space.

Although all pointers account for only 8 bytes, different types of variables make up a different number of bytes. And you rent a house why know the address, still need to know the same apartment!

Conclusion: Define what type of pointer should point to what type of variable.

----------------Arrays and Pointers---------------

1, one-dimensional array pointers

Pointer to a one-dimensional array

int a[5] = {1,2,3,4,5};int *p = A;

2, two-dimensional array pointers

Pointer to a two-dimensional array

int arr[1][3] = {1,2,3};int (*p) [3] = arr;

Attention:

The array name A does not represent the entire array, but represents the address of the first element of the array.

3. Array of pointers: Each element of an array is a pointer

or an array of pointers, that is, an array of pointers.

4. Operations between pointer variables

Prerequisite: Two pointers must point to the same array

Arithmetic: Subtraction

Essence: Calculates the relationship between two pointers and determines the position of the pointer

p = A; P1 =&a[3]

P1-p > 0 means p1 in high position

P1-p < 0 means p1 in low position

P1-p = 0 means that P1 and P point to the same position

Attention:

The operation of a pointer cannot be added, multiplied, or removed.

5. Array name access to two-dimensional arrays

int A[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};//accesses the first element address of the first row means: A + 1 =  &a[1]  =  a[1]  =  & Amp;a[1][0]  =  * (A + 1)//How to get the first element value of the first line: *a[1] =  

----------------Pointers and strings

A, string pointer, used to hold the string

Char *s = "AGADSF"; There are

B. Save the string with a character array method

Char ch[]= "DAFASDF";//

c, the difference between a string pointer variable and a character array

1, difference one, can re-assign the problem

Char ch[20]= "xxxxx";

ch = "ASDFASDF"; No, CH is a constant, storing the first address of an array.

Char *ss= "xxxxxx"; "XXXXXX" 0x01

SS = "XXX"; Yes, the SS is a pointer variable,

2, difference Two, the difference of storage

Char ch[10]= "XXXX"; Storage is stack area (readable and writable) x x x x 0 0 0 0 0 0 0

ch[4]= ' A '

Char *str = "PPPP"; Store a constant area (read-only)

Dark Horse programmer--c The language of pointers-my 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: 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.