0

I need to convert a string to 2d array (character table). I can do it in main function, but I need to create a function that does the same. Here's working code in main function:

string text= "Some_text_string"; 
//Here, I set rows and cols manually, but i want to get a "rubber" array later
int rows = 4, cols = 5;
char table[rows][cols];
    int l = 0;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            table[i][j] = text[l];
            l++;
        }     
    }

That gives me correct result, but i wanna do it like that:

table = MySuperFunction(text);

The case is to create and call a function that converts text string to an array (in fact I need a table with charachers). After that i want to rotate this table (transponate and other stuff) Maybe char array is not the best choice? so can you help me solve that?

I tried to create a function with *char type like char* func(string input), pulling my code inside and return char array but i realize thats stupid. I really cannot understand how do pointers works yet so please be patient.

artsoleg
  • 1
  • 1
  • *Here's working code in main function* No, code is invalid. C++ does not allow variable length array (`int rows = 4, cols = 5; char table[rows][cols];`). – Serge Ballesta May 28 '23 at 08:11
  • In C++, the object for dynamic arrays is `std::vector<>`. Note that a function can return any object by value, but there is an exception for the raw array from C which cannot be returned (and also cannot be received as parameter, it is always decayed into a pointer). – dalfaB May 28 '23 at 09:55

0 Answers0