C + + Two-dimensional arrays do function parameters

Source: Internet
Author: User

The two-dimensional array function parameters are as follows:

/ for a m row n column int element of the two-dimensional array // function f formal Parameter form f (int  daytab[m][n]) {...} // the following two types of rows can be ignored (f (int  daytab[][n]) {...} F (int (*daytab) [n]) {...}

It is not possible to ignore the size of the second dimension. The main reason is that the memory allocated by the two-dimensional array in the stack is contiguous, each line of it has the same element, so that array[i][j] and * (array +i) +j are the same, the program is aware that array+i I actually offsets i*n units, This also results in a two-dimensional array of array[3][3], using subscript array[2][1] and array[1][4] as the same element of access, although the latter's subscript is illegal for a 3*3 matrix, but this does not affect access.

Represents a two-dimensional array with pointers to a pointer, dynamically allocating the form of memory:

Such a method can make the size of the second dimension (that is, each row) not necessarily equal, strictly speaking, is not a two-dimensional array, but there are corresponding benefits. The memory allocated in this way can be directly accessed with a pointer pointer as a function parameter, and in the child function with double subscript.

#include <iostream>#include<stdio.h>void  out(Double**a,intMintN) {    intI, J; Doubleb=0.0;  for(i=0; i<m; i++)    {         for(j=0; j<n; J + +) {A[i][j]=b; b+=1.2; printf ("%5.1f", A[i][j]); } std::cout<<Std::endl; }   }intMainintargcChar*AGRV) {    intI, J, m=2, n=3; Double**A; A=New Double*[M];  for(i=0; i<m; i++) A[i]=New Double[n];  out(A,m,n); return 1;}

C + + Two-dimensional arrays do function parameters

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.