jeudi 3 septembre 2020

Why I cannot use the operator [] on a pointer of std::array< int, 3 > to index value?

Code:

    void function(array<int,3> * ptr)
    {
        int temp = ptr[2]; // Error

        int temp = (*ptr)[2]; // Can work!
    }

    int main(void)
    {
        array<int,3> arr={1,2,3};
        function(&arr);
        return 0;
    }

I have no idea why the pointer cannot work like this:

    int *a = new int[3];
    a[2];

Thanks for your answer.

Aucun commentaire:

Enregistrer un commentaire