function - C++ multidimensional array and pointers to table of pointers -


here thing. can understand concept of multidimensional array (let's consider 2d while) made pointer array pointers , on...

we this:

// can use dynamically defined size n , m int n = 3, m = 5 ; int **t = new int *[n]; (int = 0 ; < n ; ++i)     t[i] = new int[m]; 

what got is: (check if i'm right here)

  • 3 blocks of memory of 5 ints, placed somewhere in memory
  • one additional block of memory of same size number of blocks of ints (number of rows). block array of pointers (usually 4 bytes pointer int) int rows.
  • what interested in - t of type (**t) - pointer pointer. pointer array of pointer, because in c++ array in fact pointer pointing block of memory t[] or t[0] means *t, , t[x] means *(t+x).

now problem when sth this:

int n = 3, m = 5 ; int t[n][m] ; 

what we've got not w have doing thing showed before. sth strange. t? when printfing t, same value t[0]. looks reserved block of ints sized n*m without additional array of pointers rows.

my question is: compiler remembers dimension of array , number of rows , columns? , when asking t[i][j] asks *(t+i*n+j) n stored somewhere? problem when trying pass thing (t) function. dont know why if n , m constants possible pass t pointer array function in program:

#include <stdio.h> const int n = 3, m = 4 ; // constant! void add_5(int k[][m]) {     (int = 0 ; < n ; ++i)         (int j = 0 ; j < m ; j++)             k[i][j] += 5 ; } int main() {     // n x m array straight forward method     int t[n][m] ;     (int = 0 ; < n ; ++i)         (int j = 0 ; j < m ; ++j)             t[i][j] = i*m + j ;      (int = 0 ; < n ; ++i)     {         (int j = 0 ; j < m ; j++)             printf("%d ",t[i][j]) ;         printf("\n") ;     }     printf("\n") ;      // adding 5 cells     add_5(t) ;     printf("it worked!!\n") ;      (int = 0 ; < n ; ++i)     {         (int j = 0 ; j < m ; j++)             printf("%d ",t[i][j]) ;         printf("\n") ;     }      int ss ;     scanf("%d",&ss) ; } 

but if n , m aren't constant cant. need pass dynamically created multidimensional array's pointer function without manually allocating memory that. how this?

in c++ array in fact pointer pointing block of memory

absolutely not. array entirely separate pointer. reason might have confusion because of standard conversion called array-to-pointer conversion. consider:

int arr[10]; 

the variable arr denotes array. it's not pointer @ all. happens in many circumstances, name of array converted pointer first element. is, conversion turns int*.

int t[n][m] ; 

in case, t "array of n array of m ints". mentioned printing both t , t[0] give same result, , due array-to-pointer conversion.

  1. the expression t can converted pointer first element; is, int (*)[m], because first element of t array m elements.

  2. the expression t[0] can converted pointer first element of first subarray. pointer element t[0][0] of type int*.

these pointers hold same address because of way array laid out in memory. address array begins @ same address first element of array. however, pointers not behave in same way. if increment pointer resulting t, move along next subarray. if increment pointer resulting t[0], move along next int.

it might @ diagram of how 2d array laid out in memory compared dynamically allocated "2d array". 3-by-3 2d array this:

  0,0   0,1   0,2   1,0   1,1   1,2   2,0   2,1   2,2 ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ │ int │ int │ int │ int │ int │ int │ int │ int │ int │ └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘ 

whereas if dynamically allocated 3-by-3 "2d array":

┌─────┐ │     │ // int** └──╂──┘    ┃    ▼ ┌─────┬─────┬─────┐ │     │     │     ┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓  // array of int* └──╂──┴──╂──┴─────┘                            ┃    ┃     ┗━━━━━━━━━━━━━━━┓                     ┃    ▼                     ▼                     ▼ ┌─────┬─────┬─────┐   ┌─────┬─────┬─────┐   ┌─────┬─────┬─────┐ │ int │ int │ int │   │ int │ int │ int │   │ int │ int │ int │ // arrays of ints └─────┴─────┴─────┘   └─────┴─────┴─────┘   └─────┴─────┴─────┘   0,0   0,1   0,2       1,0   1,1   1,2       2,0   2,1   2,2 

does compiler remembers dimension of array , number of rows , columns?

yes, if have variable array type, size of array part of type. compiler knows type of variable.

and when asking t[i][j] asks *(t+i*n+j) n stored somewhere?

the expression t[i][j] equivalent *(*(t + i) + j). let's understand does. first, array-to-pointer conversion undergone t, giving int (*)[m]. add i move along point @ ith subarray. dereferenced subarray. next, subarray undergoes array-to-pointer conversion, giving int*. add j pointer jth int object in subarray. dereferenced give int.

the problem when trying pass thing (t) function. dont know why if n , m constants possible pass t pointer array function

it's lie. you're not passing 2d array function. in fact, there's no such thing array type argument. argument int k[][m] equivalent int (*k)[m]. is, array type arguments transformed pointers.

so when call function add_5(t), you're not passing array denoted t. t undergoing array-to-pointer conversion give int (*)[m] , this pointer being passed function.


Comments