How to pass two dimensional array of an unknown size to a function

后端 未结 6 981
时光说笑
时光说笑 2020-12-14 20:49

I want to make class library, a function which its parameter is a matrix of unknown size, and the user will create his own matrix with his own size and pass it to this funct

6条回答
  •  鱼传尺愫
    2020-12-14 21:05

    use this method declare an array of pointers ex: int *a[n]; Then allocate memory for them using a for loop ex:

    for( int i=0 ; i

    Now pass the argument like normal array. ex: print_array(a,n); And print_array function looks like

    print_array(int **a,int n)//the prototype for the print_array
    {
     //access the array using index such as
    std:: cout<

    The above case is for the array of nxn incase mxn is required then allocate the memory like

    for( int i=0 ; i

    then pass the both m,n and to the function and access the array in the for loop.

提交回复
热议问题