"Two-dimensional array as function parameters" means "How to pass a two-dimensional array pointer to the function"

Source: Internet
Author: User

I saw this question in Baidu just now and answered it. This is a good question. Let's summarize it.

Declare a two-dimensional array

int matrix[100][100];

When two-dimensional arrays are passed as parameters, many people may write intfun (INT ** matrix) as soon as they come up ), in this way, there is no difference between "pointer to integer Pointer" and "pointer to integer array.

In this case, parameter passing is incorrect because Matrix is a two-dimensional array, and matrix [100] [100] is a two-dimensional array, that is, an array. That is to say, what is matrix? matrix is an array with 100 elements, but each element of matrix is an integer array instead of an integer number. This is why we should remember that "pointer to integer Pointer" is different from "pointer to integer array. Because of pointer operations, you must specify the size of the array pointed to by each element in the matrix when passing parameters. Otherwise, you cannot perform pointer operations. I * n + J, that is, you need to explain the N value. Otherwise, a blind multiplication of a number will definitely make an error.
Therefore, the two-dimensional array as a parameter should be written in int (* matrix) [100]. Note that the parentheses should not be dropped. If so, because [] has a higher priority *, therefore, matrix becomes a pointer to an integer pointer. So the correct function parameter should be written like this: int fun (INT (* matrix) [100]), just write it as fun (matrix) during the call!
Then you may have a question: why can one-dimensional array parameters be written as int fun (int * A), because it has the same meaning as a [n, all pointer to integer.

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.