C and Pointers to learning notes (3)

Source: Internet
Author: User

Chapter 8 Arrays

1. Arrays and pointers

The array name is a pointer constant , which is the address of the 1 element of the array.

int a[10];  int b[10]; int *c;

(1) c = & A[0]; &a[0] represents a Pointer to an array of 1 elements.

(2) C=a; with the C = & A[0]; equivalence

(3) B = A; illegal , you cannot use an assignment to copy all elements of an array to another array, you must use a loop to copy one element at a time.

(4) a = C; illegal ,a is a pointer constant, and C is a pointer variable.

2. the subscript reference and the indirect access are identical.

Array[subscript] <==> * (array + (subscript)) ( fully equivalent )

eg:int array[10];

int *ap=array+2;

(1) AP : <==> array + 2 <==> &array[2]

(2) *ap : <==> array[2] <==> * (array+2)

(3) AP [0] : <==> * (AP + (0)) <==> array[2]

(4) AP + 6 : <==> array + 8 <==> &array[8]

(5) *ap + 6 : <==> array[2]+6

(6) * (AP + 6) : <==> array[8]

(7) AP [6] : <==> array[8]

(8)ap[-1] : <==> array[1]

(9) 2[array] : <==> * ((array)) <==> * (array + 2)

3. representation of string constants

(1) char message1[] = "Hello"; Initializes an element of a character array

(2) Char *message2 = "Hello"; true string constants

The pointer constant message2 is initialized to the storage location that points to the string constant.

    1. 4. sequential storage of array elements

eg : int array [3][6];

Array:

  


5. int matrix [3][10];

Matrix the value of the name is a pointer to its first 1 elements , so the matrix is a pointer to an array containing a total of ten integers.

(1) * (Matrix + 1) : pointer to integral type. Sub-array the address of the 1 element .

(2) * (Matrix + 1) +5 : The address of the 6 element .

(3)* (* (Matrix + 1) + 5):<==> Matrix [1][5]

6. Pointers to arrays

int vector[10], *vp = vector; correct

int matrix[3][10], *mp = Matrix; Error

Because thematrix is not a pointer to an integral type, it is a pointer to an array of integers .

Int (*p) [ten]; declares a pointer to an array of integers, andp is a pointer to an array of integral types

int (*p) [Ten] = Matrix; // declaration + initialization

Point to the 1 integral elements of the matrix:

(1) int *pi = &matrix [0][0];

(2) int *pi = matrix[0];

7. Pointer compatibility

(1) int *pt;

int (*PA) [3];

int AR1 [2] [3];

int AR2 [3] [2];

int **p2;

The

PT = & AR1 [0] [0];

PT = ar1 [0];

PT = AR1; illegal ,pt points to an int value , while ar1 points to 3 int values constitute the Array

PA = ar1; all point to Int[3]

PA = ar2; illegal ,pa points to an array of 3 int , and ar2 points to a 2 a int Array of Components

P2 = &pt; all point to int *

*P2 = ar2 [0]; The *P2 type is a pointer to int , so it is compatible with ar2 [0]

P2 = ar2; illegal ,p2 is pointer to pointer,ar2 is pointed by 2 int A pointer to the array that the value consists of.

Therefore,the P2 and ar2 types are different .

(2) It is legal to assign the address of a constant or very data to a pointer to a constant . Only the address of a very large amount of data can be assigned to a normal pointer .

int *p1;

const int *P2;

const int **PP2;

P1 = p2; illegal , assigning a const Pointer to a non- const pointer

P2 = p1; legal, assigning a non- const pointer to a const pointer, provided that only one layer of indirection is performed

PP2 = &p1; illegal , assigning a non- const pointer to a const pointer

C and Pointers to learning notes (3)

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.