1

Trying to access static array in function fill() by passing its address as an argument.

#include <iostream>
using namespace std;

void fill(int val, int n, int m, int **arr){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            arr[i][j] = val;
            arr[i][j] = val;
        }
    }
}

int main(){

    int n = (cin>>n, n);
    int m = (cin>>m, m);

    int arr[n][m];
    fill(1, n, m, (int**)arr);
}

If we are not allowed to access arr[n][m] 2-D array outside the main() function by casting it into double-pointer, then what is the right way to access that memory outside main()?

Solution:

void fill(int val, int n, int m, int *arr){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            *(arr + i * m + j) = val;
        }
    }
}

int main(){

    int n = (cin>>n, n);
    int m = (cin>>m, m);

    int arr[n][m];
    fill(1, n, m, &arr[0][0]);
}
tusharRawat
  • 641
  • 5
  • 14
  • I think this might work, call : fill(1, n, m, &arr[0][0]); and access fill(int val, int n, int m, int *arr){ *(arr + i * m + j) = val; } – tusharRawat Feb 07 '20 at 14:59

2 Answers2

1

int arr[][] isn't an int**.

If you need dynamic array, you can use vector's:

#include <iostream>
#include <vector>

using namespace std;

void fill(int val, int n, int m, vector<vector<int>>& arr) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      arr[i][j] = val;
      arr[i][j] = val;
    }
  }
}

int main() {

  int n = (cin >> n, n);
  int m = (cin >> m, m);

  vector<vector<int>> arr;
  arr.resize(n);
  for (auto row = arr.begin(); row != arr.end(); ++row) {
    row->resize(m);
  }
  fill(1, n, m, arr);
}
Evgeny
  • 721
  • 3
  • 5
  • thanks, @Evgeny, but STL is not allowed have to code in std c/c++ way using static memory allocation. – tusharRawat Feb 07 '20 at 14:39
  • 1
    You can use code, like this: ```int **array; array = new int *[10]; for(int i = 0; i <10; i++) array[i] = new int[10]; void passFunc(int **a) { // ... } passFunc(array);``` from here: https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – Evgeny Feb 07 '20 at 14:41
  • why is it working in dynamic memory allocation and not in static? – tusharRawat Feb 07 '20 at 14:44
  • 3
    @tusharRawat It works for both. But since their are not the same types, you cannot mix them. If you write a function that expects a pointer of pointer, you can't provide something that is not. – Fareanor Feb 07 '20 at 14:50
  • @tusharRawat, just because something works it doesn't mean you should use it, allways look for the best solution. Also what is it with using `int m = (cin >> m, m)`? Why do you need m twice, and why dont you just the normal thing `cin >> m >> n`? – anastaciu Feb 07 '20 at 15:09
  • @anastaciu, int m = (cin >> m, m); is same as int m; cin>>m, it's just oneliner, declaration and input in the same line. – tusharRawat Feb 07 '20 at 15:25
  • @tusharRawat Ok then, I was just curious, don't see that usage a lot. – anastaciu Feb 07 '20 at 15:27
  • *don't see that usage a lot.* -- I have never seen usage like that in any C++ code base. Looks like obfuscated, "hey look what I can do" coding. – PaulMcKenzie Feb 07 '20 at 15:46
  • @PaulMcKenzie I can't say I remember, but never is too definitive for me. – anastaciu Feb 07 '20 at 17:43
1

Regarding the variable sized arrays read Why aren't variable-length arrays part of the C++ standard?

Since you are using C++, I would advise one of the containers provided by the language to store your data, in this case a vector of vectors.

Live Sample

#include <iostream>
#include <vector>

using namespace std; // for test purposes shouldn't be used

void fill(int val, int lines, int cols, vector<vector<int>>& matrix){
    for (int i = 0; i < lines; i++){
        vector<int> v;
        for (int j = 0; j < cols; j++)
            v.push_back(val);
        matrix.push_back(v);
    }
}

int main() {

    int lines, cols; 
    vector<vector<int>> matrix;

    cout << "lines and columns: " << endl;
    cin >> lines >> cols;

    fill(1, lines, cols, matrix);
}

If you must use arrays, what I would consider a better way is to allocate memory dinamically, C style:

Live Sample

#include <iostream>   

using namespace std; // for test purposes, shouldn't be used

void fill(int val, int n, int m, int *arr){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            *(arr+i+j) = val;
        }
    }
}

int main(){

    int n, m;
    cout << "lines and columns: " << endl;
    cin >> n >> m;

    int* arr = (int*) malloc(sizeof(*arr) * n * m) ;
    fill(1, n, m, arr);
}
anastaciu
  • 20,013
  • 7
  • 23
  • 43
  • 1
    yeah got it, works when first element address is passed and access memory using pointer manipulation *(arr + i * cols + j). – tusharRawat Feb 07 '20 at 15:01