Good day! A huge request to throw a link on the topic or immediately code. In general, I can’t figure out how to work with several two-dimensional arrays of type char using one function.

Example: there are 2 arrays

char a1[13][29]; char a2[13][14]; 

there is a function to which you need to pass one of them .

 func(char a[][]); 

The problem is that if the argument in the function is written like this, func(char a[][29] ); I can only use it for the a1 array, but not for a2 .

How do I need to pass an array to a function so that it doesn't matter what dimension it will be?

    2 answers 2

    Want some good advice? If you write in C ++, avoid native arrays, they are unintuitive. With my opinion I agree, by the way, the official FAQ for C ++ .

    Write like this:

     #include <vector> using namespace std; // ... vector< vector<char> > a1, a2; void func(vector< vector<char> >); 

    Everything will work.


    With such syntax, as you propose, does not work, and can not in principle. The fact is that the procedure does not know with which step to increase the address when moving to a new line.

    You can make an ugly patch, passing the pointer to the first element and the length of the string, and calculating the addresses manually:

     (i, j) -> *(pFirst + (i * lineSize) + j) 

    But this is the last century, use standard containers.

    • I never used it, but I tried to figure it out. A bit incomprehensible how can I initialize it? Suppose declared a1: vector <vector <char>> a1; And how directly in the code to make these containers be analogous, initialize the array like this: char a [2] [3] = {"abc", "def"}; - iterq
    • @iterq: Have you tried it yourself? Here is the code : vector <vector <char>> vv = {{'a', 'b'}, {'c', 'd'}}; for (auto & v: vv) {for (auto & n: v) cout << n << ""; cout << endl; } - VladD
    • Yes, I tried, but the same error with your code: The compiler underlines the 1st brace (this one { {a ',' b '}, {' c ',' d '}};), and when you hover it displays the inscription "Initialization with {...} is not allowed for object of type ...". This actually stopped me) - iterq
    • @iterq: it means you have an outdated compiler. g ++ 4.7.2 copes . --- In this case, you have to manually: vector <char> firstLine (2); firstLine [0] = 'a'; firstLine [1] = 'b'; #define ARRAYSIZE (arr) (sizeof (arr) / sizeof (arr [0])) char [] initializer = {'c', 'd'}; vector <char> secondLine (initializer, initializer + ARRZYSIZE (initializer)); vv [0] = firstLine; vv [1] = secondLine; or something else so elegant. - VladD
    • I do everything in VS C ++ 2010 .. strangely, there have never been such problems. In any case, thank you very much for the information, I think I will sort it out further. - iterq 2:44 pm

    so you can still pass

     include <iostream> using namespace std; void f(char * p, int len); int main(){ char a[2][2] = {'a', 'b', 'c', 'd'}; char b[3][3] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; f(&a[0][0], 4); cout << endl; f(&b[0][0], 9); return 0; } void f(char * p, int len){ for (int i = 0; i < len; i++){ cout << *p << " "; p++; } } 

    pointer access should be quick like

    • This will roll if you do not have to take into account the line boundaries - VladD