arrays declaration and addressing

Posted by avinash on Stack Overflow See other posts from Stack Overflow or by avinash
Published on 2012-09-09T09:28:02Z Indexed on 2012/09/09 9:38 UTC
Read the original article Hit count: 157

Filed under:
|
|

I have a few straightforward questions:-

  1. Is the following correct according to a normal c++ compiler?

    int arr[3][4];
    void func(int *a, int m, int n)
    {
     int i,j;
     cin>>i>>j; 
     cout<< a[i*n + j]; //is this way of addressing correct provided 0<=i<m and 0<=j<n
    } 
    
    int main()
    {
     func((int*)arr, 3,4);
    }
    
  2. If the bounds of an array strictly has to be a constant expression, why doesn't the following generate compiler errors?

    int func(int m, int n)
    {
     int arr[m][n]; //m and n are not known until run time
    }
    

© Stack Overflow or respective owner

Related posts about c++

Related posts about compiler