14- arrays and pointers

& Addressing operator

* Pointer operator

& Addressing operator and * pointer operator

It has the same priority, from right to left binding.

int a;

Int *p=&a;

*&a---->a

&*p---->p

 

int *p=&a;  

Pointer assignment:

When it was declared: assignment int * p = & a;  

First statement by assignment: int A; int * the p-; the p-& = A;

* p occur when the individual is the data.

int * p preceded by a data type.

 

A one-dimensional arrays and pointers.

1, one-dimensional array address.

It is the address of the first element of the array.

 

1 #include <stdio.h>
2 
3 int main()
4 {
5 int arr[5];
6 printf("%d\n", &arr[0]);
7 
8 return 0;
9 }

 

Definition of a pointer, the first address of the one-dimensional array of & a [0] gives the pointer ptr , said ptr pointer to the array A [. 5] .

An array array name is the first address of the array. arr [5] & arr [0 ] == arr == ptr

 

2, one-dimensional array for input and output.

A, the pointer is not changed.

 

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     //int arr[5];
 6 
 7     //for (int i = 0; i < 5; i++)
 8     //{
 9         // arr[i] = i + 1;
10     //}
11 
12 
13     //for (int i = 0; i < 5; i++)
14     //{
15         // printf("arr[%d]=%d\n", i, arr[i]);
16     //}
17 
18 
19     int arr[5];
20     intARR = p *;    // int * p = & ARR [0]; 
21 is  
22 is      for ( int I = 0 ; I < . 5 ; I ++) // I 0-4 
23 is      {
 24          // after long pointer variable p is declared finished address * p is to accompany the address of the variable pointed. 
25          * (P + I) = I + . 1 ; // ARR [0] - P * ARR [. 1] - * (P +. 1) ARR [2] - * (P + 2) 
26 is      }
 27  
28       
29      for ( int I = 0 ; I < . 5 ; I ++ )
 30      {
 31 is          the printf ( "arr[%d]=%d\n", i, *(p + i));
32     }
33 }

 

* Only when expressed in a statement that is a pointer to any other situation alone *   have said pointer operator.

 

B, the array index is a pointer p is changed, it must be put before printing p redirected to the array of the first address.

 1 int arr[5];
 2 int *p = arr;   // int *p=&arr[0];
 3 
 4  
 5 /********************************
 6 1: *p         -- arr[0]    1
 7 2: *(p+1)     -- arr[1]    2
 8 3: *((p+1)+1) -- arr[2]    3
 9 ********************************/
10 
11 printf("赋值之前:%d\n", p);
12 
13 for (int i = 0; i < 5; i++) //i 0-4
14 {
 15      * p ++ = I + . 1 ; // Can 1-5 p is copied to the array changes  
 16      // value of an expression which is the first calculation expression p ++ in +. 1 to p
 . 17      @ ++ p p + 1 then give calcd expression 
18 is  }
 . 19  
20 is the printf ( " after this assignment:% D \ n- " , P);
 21 is  
22 is  for ( int I = 0 ; I < . 5 ; I ++ )
 23 is  {
 24      the printf ( " ARR [D%] D =% \ n- " , I, ARR [I]);
 25  }
 26 is  
27   
28 P = & ARR [0 ]; // first address of the p redirected array 
29 the printf ( " before printing:% D \ n- " , p);
 30  
31 is   
32  for ( int I = 0 ; I < . 5 ; I ++ )
 33 is  {
 34 is      the printf ( " ARR [D%] D =% \ n- " , I, P * ++ );
 35  }
 36  
37 [ the printf ( " after printing:% D \ n- " , P);

 

 

Recommendation: meet pointers: 1 , the pointer to type?  2 , where the pointer points?  3 pointer own type?

 

Second, the two-dimensional arrays and pointers.

1, address two-dimensional array.

 

 

 

First Address:

&a[0][0]  a[0]  &a[0]  a   &a

 

行地址:

1、0行的地址就是首地址

2、1&a[1][0]   a[1]   &a[1]  a+1

3、第2&a[2][0]   a[2]   &a[2]  a+2

4、第i行  &a[i][0]    a[i]   &a[i]   a+i

蓝色的加法就加一维数组的字节数 橙色的加法加数据类型的字节数

 

通过行地址去找到nm列的元素。

&a[n][m]   a[n]+m  (关键是要搞清楚 以一维数组老做判断 还是以整体的二维数组来进行判断 )

 

//nm列的元素

*(a[n]+m)

*(*(a+n)+m)   //a+n  a[n]  *(a+n)   a[n]+m

 

下节课:

字符串和指针(数组和指针)  指针类型的数组

函数和指针

指针总结

结构体

预处理

位运算

数据结构

写项目

 

 

 

Guess you like

Origin www.cnblogs.com/tiantiancode/p/11131766.html