Writing a library - returning a char array

Hi,

i am currently writing a library, and have the following issue.
i have a class (lens) that contains the manufacturer name as a char array.

I know i cant just return the array, but since the variable itself is private, i also cant return a pointer.

how do i return the manufacturer name?

class lens {
    public:
        bool setManufacturer(uint8_t* manufacturerName[15]);
        [...]
        uint8_t* getManufacturer(); //this obviously won't work
    private:
        uint8_t * manufacturer[15];
        [...]
}

Why not just make the array public?

it's pretty common practice that such a function would be passed a char array to return the string in, relying on the calling function to allocate the buffer

1 Like

Here are two options:

  1. Make it public
  2. Rewrite your getter function like this:
inline size_t getManufacturer(uint8_t *dst_buffer, size_t buffer_length) {
  return strlcpy(dst_buffer, manufacturer, buffer_length);
}

For option#2 you have to have dst_buffer ready:

uint8_t buff[32];
Lens->getManufacturer(buff, sizeof(buff));

Other options are:

return a strdup() of the manufacturer. In this case you have to free() the returned value after use:

inline uint8_t *getManufacturer() {
  return strdup(manufacturer);
}


...
...


uint8_t *buf;
buf = Lens->getManufacturer();
...
...
free(buf);

should be

bool setManufacturer(const uint8_t* manufacturerName) {
  if (!manufacturerName)
    manufacturerName = "<null>";
  strlcpy(manufacturer, manufacturerName, sizeof(manufacturer));
  return true;
}

i want to make sure it can only be set by calling the set function because i need to make sure the input is valid

sounds like thats the easiest solution to implement

You can return a const pointer to the array back to the calling function. That way it can only be used to read from the array, not modify it.

Note you have somewhat of a bug : You need to store manufacturer as an array of uint8_t (why not char ?), not uint8_t*, otherwise each element is a pointer and returning it safely becomes more complicated.

to your question, and as suggested by @gfvalvo, You can return a pointer to the internal array as const uint8_t* const, which prevents the caller from modifying the data or the pointer itself. For your class, it would look like this:

class lens {
  public:
    bool setManufacturer(const uint8_t* name) {
      ...
      return true;
    }

    const uint8_t* const getManufacturer() const {
      return manufacturer;
    }

  private:
    uint8_t manufacturer[15];
};

hi, thanks for pointing out the bug,

i am not using a char because while the values are actually hex encoded ASCII chars, they never get interpreted as such, so i chose uint8_t

That's a very inefficient. It requires 16 bits to represent 8 bits of information.