Basic knowledge of C + +--two-D array for function parameters

Source: Internet
Author: User

1. Determining the size of a two-dimensional array


void Func (int array[3][10]);


void Func (int array[][10]);
2. Two-dimensional arrays of indeterminate size are converted to two-dimensional pointers:
Arguments are passed to the starting address of the array, stored in memory by array rules (stored in rows), and not by branches and columns, so it can be converted to a two-dimensional pointer.
void Func (int **array, int m, int n);


Call to convert the array name to a two-dimensional pointer


Func ((int**) A, 3, 3);


Get specific array elements


* ((int*) array + n*i + j);

: Where (int *) array converts an array to the storage of one-dimensional arrays
The elements are then read from a one-dimensional array.


If * (array + n*i + j); Output:


00000001
00000002
00000003
00000004
00000005
00000006
00000007
00000008
00000009
0000000A
0000000B
0000000C
0000000D
0000000E
0000000F
00000010


If * ((int*) array + n*i + j); Output:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


3. The most convenient is to switch to one-dimensional pointers.

Because it is stored continuously in memory, so ...

void Func (int *array, int m, int n);


Call to convert the array name to a two-dimensional pointer


Func ((int*) A, 3, 3);


Get specific array elements


* (array + n*i + j);

Basic knowledge of C + +--two-D array for function parameters

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.