0

Let's say I have the following code:

void func(const char *arr[])
{
    std::cout << sizeof(arr) << ", " << sizeof(char*) << std::endl;
}

int main()
{
    const char *arr[] = { "aaa","bbb","ccc" };
    std::cout << sizeof(arr) << ", " << sizeof(char*) << std::endl;
    func(arr);
    return 0;
}

The output is:

24, 8
8, 8

Ok, I understand that char *[] will be implicitly converted into char **. But I think that in case of implicit conversion should be a way of explicit avoiding such a situation. Otherwise I see 2 idempotent (or looks like idempotent) expressions that are completely different in fact.

How to definitely avoid this conversion? I expect to get the same 24, 8 in the function.

P.S. A know about vector etc. I'm just interested in the meaning of having such ambiguity.

folibis
  • 12,048
  • 6
  • 54
  • 97
  • 2
    What exactly are trying to avoid? I'm afraid that isn't entirely clear to me. Declaring function parameters? the conversion? – StoryTeller - Unslander Monica Oct 10 '21 at 13:08
  • Yes, the conversion. I expect to get the same `24, 8` in the function. – folibis Oct 10 '21 at 13:12
  • I think you mean "equivalent" rather than "idempotent". I'm not sure if this is the solution you're after but you can have the function take a reference to an array, which then won't be converted to a pointer. – interjay Oct 10 '21 at 13:16

0 Answers0