C language Pointer 1

Source: Internet
Author: User

A: 1, Address: The number of memory unit

The value of the memory unit: What is stored in the memory unit

The memory cell can be found by address and then the content is removed

2, the pointer: is the address

3. There are several ways to make data access to variables

Two kinds: direct access int a=10; a=a+10;

Indirect access via pointers

Two: 1, pointer variable

Is the variable used to store (the pointer) address

Note: pointers (addresses) and pointer variables are different

Pointer variable usage flow: First define---> Initialize-use

2. Pointer variable definition

Format: Data type * pointer variable name;

Pointer variable name strictly follows identifier naming specification

int A;

char c;

Double D1;

int *p; Description: Defines a pointer variable whose name is P, which can hold the address of an integer (int) data

Attention:

1) pointer variable can only store address

2) int *p, "*" means defining a pointer variable

3) Pointer variable also has scope (global, static, local) static modified local variable life cycle extension

4) To define pointer variables, note the type of the pointer, because when you use the pointer action pointer to the memory cell content, the type of pointer is addressed in memory, so problems may occur.

3. Initialization of pointer variables

It can be understood that the pointer variable points to the variable whose address is

1) Simultaneous initialization of the definition

"&" Use this symbol to get the address of a variable

int A;

char c;

Double D1;

int *p1=&a; &a gets the address of the variable a scanf ("%d", &a);

Char *c1=&c;

Double *dp=&d1;

int *p2 = &c; (wrong, p2 can only hold an address of int type data)

int *p3 = 100; (wrong, P3 is used to store the address, cannot hold the number of integral type

2) Define first, then initialize

1) Initialize the address of variable variables

int *p3;

P3 = &a; Initialize the P3 pointer

2) Initialize another pointer with a pointer variable

int *p4=&a;

int *p5;

P5 = P4; P4 when storing an address, you can assign the value directly to P5

3) pointer variable can be initialized to null

Null empty (not 0 "")

int *P2 = NULL

Assign a value of 0 to NULL

int *p3=0;

Error:

int *p6=&a;

Char *c2=&c;

int *p7 = C2; Wrong, because P7 can only hold the address of the int type data C2 is the address of a char type variable

Three: reference to pointer variable:

& Get the address of a variable

* Two types of usage

1) When defining a pointer variable, a pointer variable representing the defined

2) * Pointer variable name means the value of the storage unit that corresponds to the address stored in the pointer variable

int a = 9; int *P1 = &a;

*P1 9,//Get the address of a, corresponding value

Four: pointer usage scenarios:

Use scenario 1 for pointers:

In the modulated function, access the variables in the keynote function

For example: void change (int *p) {

*p = 100;

}

Application Scenario 2 for pointers:

A function can have multiple return values (in principle with the pointer using scenario 1)

For example: void Yunsuan (int a,int b,int *add,int *jian,int *cheng,int *chu) {

*add = a+b; *add access to the contents of the corresponding storage unit of the address that the Add pointer variable holds

Add = 0x01 *0x01 Gets the value of the address

*jian = A-B;

*cheng = a*b;

*chu = A/b;

}

V: Multi-level pointers:

int a = 10; int p = &a; int **P2 = &p; int ***P3 =&p2;

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

printf ("A addr =%p\n", &a); Address of a

printf ("*p =%d\n", *p); 10

printf ("p addr =%p\n", &p); The address of the pointer variable p

printf ("*P2 =%p\n", *P2); The contents of P, the address of a

printf ("**P2 =%d\n", **P2); Ten * (*P2) = * (Address of a) = 10

printf ("***P3 =%d\n", ***P3); Ten * * (*P3) = * (*P2) = *p = 10

Six: What is the pointer? To differentiate between types:

Doubt Point:

1) data occupies a different number of bytes, but why the pointer is 8 bytes

The pointer variable is used to hold the address, the address occupies the same number of bytes,

2) Since the pointer variable takes up 8 bytes, why do we have to distinguish the pointer type?

int a = 266;

Char *pc = &a;

printf ("*pc =%d\n", *pc);//Printing results a problem? Because a pointer of type char is addressed in memory, addressing a byte of memory, and a variable of type int takes up four bytes;

Seven: 1, the concept of pointers to arrays:

Pointers to array elements

Defines a pointer variable that points to an element of an array

Attention:

int a[5];

int *p = &a[1];

int *P1 = &a[0]; &a[0] = = the first address = = array name of the array

P1 points to the array.

2. Referencing (accessing) array elements with array pointers

The concept of introducing array pointers is primarily to

, referencing each element of an array with pointers (in fact, using pointers to indirectly access a[0] a[1] ..... )

1) referencing the elements of an array with an array name

A[0] a[1] .... (Direct access with array name)

Accessed using the array's first address (array name)

A+0-a[0] a+1--a[1] a+2--->a[2]

2) referencing the elements of an array with pointers

Accessing array elements with array pointers

int *p=a;

int *p1=&a[0];

P+1 A[1]//a+1

P+2 A[2]

3) Wonder: Why use pointers if you can access arrays with array names and arrays of pointers?

Use pointers to be more flexible

Reflected in:

for (int i=0; i<3; i++) {

printf ("p+%d =%d\n", i,* (p++)); *p p = p+1 *p-->a[1]

}

4) The difference between the array name and the pointer variable

int a[10]; A array name is a constant a+1 a+2;

int *p = A; P is a pointer variable, p = p+1 Yes, p++

Note: 1>p+1 refers to the offset of the address to which the pointer variable is pointing, if the int row pointer variable is offset by 4 bytes

2> array a++ is incorrect because the array name A is a constant and cannot be a=a+1,

Eight: array of pointers:

Each element in the array is an address (pointer)

int a[3][3]={1,2,3,4,56,7,8,120,11};

Defines a pointer array named PA with three elements, a[0],a[1],a[2] three addresses

int *pa[3]={a[0],a[1],a[2]};

* (pa+1)

Note: An array pointer is a pointer to an array that is an array of pointers (addresses)

C language Pointer 1

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.