vendredi 28 août 2015

How to read the contents of a C-style array with only a pointer to it?

Let's say I have :

class foo {
private:
    int data[5] {1, 2, 3, 4, 5};

public:
    int* getpData () {
        return data;
    }

    unsigned int getDataByteLength () {
        return 5 * (sizeof(unsigned int));
    }
};

I don't have access to data and I want to compute the sum of all of data elements.

How do I loop data and sum it's elements if I only have a pointer to it ?

Aucun commentaire:

Enregistrer un commentaire