Use pointers to reference arrays.

Source: Internet
Author: User

Use pointers to reference arrays.
(1) pointer operation when referencing array elements

If the pointer Variable p points to an element in the array, p + 1 points to the next element of the same array, P-1 points to the previous element of the same array.

Note: P + 1 does not simply add the value + 1, but the number of bytes occupied by the element.

Int, float, and long all occupy 4 bytes, and char occupies 1 byte.

(2) * p ++, * (p ++), * (++ P), ++ (* p), * (p --), * (-- p) differences

* P ++ executes * p first and then points to the next element.

* (P ++) is the same as * p ++, because ++ and * are of the same priority, and the combination direction is from right to left.

* (++ P) first point p ++ to the next element.

+ (* P) execute * p first, and then point p to the element value + 1

* (P --) execute * p first, and then p minus

* (-- P) indicates that p is first Auto-subtracted, and then * p is obtained.

(3) using array names as function parameters

When the variable name is used as the function parameter, the value of the variable is passed. When the array name is used as the function parameter, the address of the first element of the array is passed.

For example, to store n integers in the array in reverse order

1 # include <stdio. h> 2 int main () {3 void inv (int x [], int n); 4 int I, a [10] =, 4, 2}; 5 6 inv (a, 10); // the address of the first element of the array is transmitted as a parameter using the array name 7 for (I = 0; I <10; I ++) 8 printf ("% d", a [I]); 9 return 0; 10} 11 // the inv function is used to exchange two array elements 12 void inv (int x [], int n) {// real parameters upload the first element address of array a to the 13 int temp, I, j, m = (n-1)/2; 14 for (I = 0; I <= m; I ++) {15 j = n-1-i; 16 temp = x [I]; x [I] = x [j]; x [j] = temp; 17} 18}
(4) using pointers to reference multi-dimensional arrays

1. Multi-dimensional array element address

Assume that a two-dimensional array int a [3] [4] exists.

A + 1 indicates the first element address of a [1], that is, the address of the first element of the first line.

A [0] + 1 represents the address of the first element in row 0th.

* (A + 0) + 1 represents the address of the first element in row 0th.

* (A + 0) + 1) represents the value of the first element in the first row of 0th.

Summary:

Two-dimensional array names point to rows, and one-dimensional array names point to columns.

Add a * Before the pointer to the row to convert it into a pointer to the column, for example, * a points to 0th columns in 0th rows.

Add an & in front of the pointer pointing to the column and convert it to a pointer pointing to the row. For example, & a [0] points to 0th rows.

(5) pointer to array

Introduce an example to find all the scores of failed students in more than one course

1 # include <stdio. h> 2 int main () {3 void search (float (* p) [4], int n); // (* p) [4] is a pointer variable pointing to a one-dimensional array containing four elements 4 float score [2] [4] = }}; 5 6 search (score, 2); // score and (* p) [4] must be of the same type 7 return 0; 8} 9 10 void search (float (* p) [4], int n) {11 int I, j, flag; 12 for (j = 0; j <n; j ++) {13 flag = 0; 14 for (I = 0; I <4; I ++) 15 if (* (p + j) + I) <60) flag = 1; 16 if (flag = 1) {17 for (I = 0; I <4; I ++) {18 printf ("% 5.1f ", * (p + j) + I); 19} 20 printf ("\ n"); 21} 22} 23}

 

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.