-2

I have an array initialised like the following:

int example[5][5];
example[5][5] = 55;

And a function:

void example_function(auto inArray, int z){
    cout << "example text = " << inArray[z][z] << endl;
}

And I am calling it into the function like this:

example_function(example, 5);

As you can see, I have the parameter for the function as auto when it is really using an integer array.

When I use typeid(table).name() to get the type of the array example, it outputs the type as A5_A5_i where the fives are from the initialisation (e.g. int example[3][4][5] would output A3_A4_A5_i)

When using typeid(table).name() on inArray after changing the type of the parameter from int to auto, I get the type name as PA5_i which is different to the one mentioned above.

How can I get a suitable type for a parameter in my function, and is there a better way to do this

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Forp
  • 37
  • 6
  • 3
    1) Your `example_function` doesn't even compile due to `inArray` being declared as `int inArray`, and being used as `inArray[z][z]`. 2) How is the entire example is related to what is printed when using `typeid`? Please provide [mre], providing what was output, alongside your wants/expectations. – Algirdas Preidžius Jan 26 '21 at 20:29
  • I have the feeling that you expect something from `typeid` that it doesn't provide you, though I also dont understand what that is ;) – 463035818_is_not_an_ai Jan 26 '21 at 20:45
  • @AlgirdasPreidžius Thank you for your response - It just seemed that my C++ knowledge was not as great as yours so I did not know how to use my array as a parameter for the function, so I would use "auto" instead of "int", but in my original question I did not change that. Thank you for letting me know about having to create minimal reproducible examples as I usually try to do that anyway, but in this case I forgot to assign values to the array in my example :) – Forp Jan 26 '21 at 20:55
  • @Forp 1) "_I did not know how to use my array as a parameter for the function_" It stands to reason, that if one talks about "outputs" in the question, one has access to functioning C++ compiler, with which one can test their example, before posting a question about it. 2) "_Thank you for letting me know about having to create minimal reproducible examples_" If you had read the link, you would see that your example isn't [mre]. If you had taken the [tour], and read through [ask], and [help], **before asking** (as is expected), you would've known that [mre] is expected. – Algirdas Preidžius Jan 26 '21 at 21:55
  • 1
    @Forp 3) "_but in this case I forgot to assign values to the array in my example_" `example[5][5] = 55;` doesn't do that either. It assigns a value `55` to an out-of-bounds memory location, inducing undefined behavior in the process. This is the reason, why it is strongly recommended to learn from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), instead of coding randomly. – Algirdas Preidžius Jan 26 '21 at 21:57

1 Answers1

3

If the array passed to the function is known beforehand, in this case as int example[5][5];, you can use the following,

void example_function(int (&inArray)[5][5], int z){
    cout << "example text = " << inArray[z][z] << endl;
}

Here we take the array by reference to avoid array decay.

If the size might vary at runtime, use std::vector.

std::vector<std::vector<int>> example;

void example_function(std::vector<std::vector<int>> inArray, int z){
    cout << "example text = " << inArray[z][z] << endl;
}
D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • 1
    Thank you for the information! I didn't know I had to specify the dimensions of the array in the paramaters (but strangely not as an argument). I think the whole typeid thing may have distracted me too much. – Forp Jan 26 '21 at 20:57