A two-dimensional array as a function parameter in C + +

Source: Internet
Author: User

In peacetime, we often encounter the whole array as a function parameter, the case of a one-dimensional array, is to use the array name as the formal parameters and arguments, the first address of the array is passed. Two-dimensional arrays we use a lot, but there are always various problems, today I summarize

One important point is that the string "China" is an address in the compiler's eyes! The action string is carried out through the first address of the memory cell, which is the ultimate essence of the string.

If "China" is stored in memory, the 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005.

s = "China", assigned to what, address.

In fact, the real meaning is S = "China" = 0x3000;

First we re-recognize the next two-dimensional array from the point of view of the pointer, int a[3][4], where a is a row pointer, pointing to the beginning of a two-dimensional array, a+1 pointing to the second row, referring to A[1], and its type is int (*) [4]. A[i] is an element pointer, such as A[0] is the array name of the 1th row of a one-dimensional array, pointing to a[0][0],a[0]+1 pointing to a[0][1]. Its type is int*.

int (*p) [4], at this time P and a are equivalent, because both are row pointers, the type is int (*) [4].

(1) A two-dimensional array specifies the number of rows as a formal parameter, and the argument is a two-dimensional array name

void f (int a[3[4]);   void f (int a[][4]); void f (int (*a) [4]);

All three of the above are equivalent. However, the number of columns in an array cannot be omitted because the argument passes the starting address of the two-dimensional array, mainly because the memory allocated by the two-dimensional array is contiguous and each row has the same element, so that a[i][j] and * (* (a +i) +j) are the same, The program is aware that array+i I actually offsets the i*n units, which also results in a two-dimensional array array[3][3], using subscript array[2][1] and array[1][4] is the same element of access, although the latter subscript is illegal for a 3*3 matrix, However, this does not affect access. The number of columns is omitted, and memory does not know how to store the two-dimensional array.

Disadvantage: The disadvantage of this approach is that it is not convenient to fix the number of rows in the array beforehand. This method is not appropriate if my array is not known in advance.

(2) Two-dimensional array as one-dimensional array access, the form of arguments are two, but are int* type, one is *a, one is a[0], all point to the first element of a one-dimensional array

inta[2][2] = {2,3,4,5};//contiguous array of memory segments with 4 elements//void f (int p[][2], int row, int col)//This method must know in advance the size of dimensions other than the first dimension, inflexiblevoidFint*p,intRowintCOL)//convert to a one-dimensional array visit{     for(inti =0; i < row; i++)    {         for(intj =0; J < Col; J + +) {cout<<p[i*col+j]<<" "; }} cout<<Endl;}

In the modulated function, the method of addressing can be either in the program or in the * (P+I*COL+J) mode.

Here int** p =a; is wrong, because P is a int** type, and A is an int (*) [2].

Explanation for the following reasons: (1) Different types are very obvious (2) from the point of view, a is the array of a[0] address, also equivalent to a[0][0] address, if P=a, p also represents a[0][0] address, *p represents the value of a[0][0] itself, * * P accesses a memory space of address a[0][0] (here equals 2), which is not allowed.

But there's a situation where you can assign a value like this.

Char *a[2]={"Hello","World"}; char** p=a;

The type of a in these definitions is not int (*) [2], but rather int* (as stated above should be char*), p=a words, *p=a[0], so a represents the address of the first address of the array, that is, a[0]. *p-a[0] is the "Hello" in the first address, **p also means ' h '. This is legal.

(3) A two-dimensional array is passed through a two-level pointer, and the argument must be a pointer

voidSubfun (intNChar**Subargs) {      inti;  for(i =0; I < n; i++) {printf ("subargs[%d] =%s\n", I, subargs[i]); }  }    voidMain () {Char*a[3]; Charargs[][5] = {"ABC","def","Ghi"}; a[0] = args[0];//equals with a[0] = &args[0][0]; a[1] = args[1]; a[2] = args[2]; Subfun (3, a);//If this is Subfun (3, args), an error will be compiled}
voidPrintfloat**tab,intRowsintcols) {     for(intI=0; i<rows;i++){         for(intj=0; j<cols;j++) {cout<<tab[i][j]<<" "; } cout<<Endl; }}intMain () {floatta[2][3]={{1.0,2.0,3.0},{4.0,5.0,6.0}}; float**p=New float*[2];//opening the line of space     for(intI=0;i<3; i++) P[i]=New float[i];//Open up a column of space     for(intI=0;i<2; i++) {//Assign Value         for(intj=0;j<3; j + +) {P[i][j]=Ta[i][j]; }} cout<<"TA:"<<Endl; Print (p,2,3);//Print//memory release mode for P     for(intI=0;i<3; i++)        Delete[]p[i]; Delete[]p; return 0;}

In fact, the above two programs are the same way, are additional applications for a two-level pointer to the memory, and then copy the array values into this piece of memory, after the use of the memory must be manually freed. This corresponds to the two-level pointer to the function being tuned.

There is also a way to force the conversion of the arguments.

Argument delivery:

int a[3[4];  F ((int * *) A,3,4);

This allows access to the element A[i][j] in the called array using the following form:

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

Note You cannot use A[I][J] for direct access because the compiler cannot locate it.

There will be a change in addressing, such as when subscript addressing, when only the pointer addressing, for the moment I did not find the authoritative answer, not to mention. Added later.

A two-dimensional array as a function parameter in C + +

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.