rlogiacco / CircularBuffer

Arduino circular buffer library
GNU Lesser General Public License v3.0
312 stars 85 forks source link

I need the address of the array. How can I get the address of an array? #11

Closed highspeeder closed 6 years ago

highspeeder commented 6 years ago

I set a pointer to fetch the address value of the buffer, and put the address value of the buffer. However, I get the following error: "The expression must be an lvalue or function specifier"

in code:

CircularBuffer<char, 10> chars;
char* pGet = NULL;

void loop()
{
    pGet = &chars[0];
}

How can I get the address value of the buffer?

I tried changing the code as follows, but it also failed. :

pGet = &chars;
rlogiacco commented 6 years ago

Your last attempt returns a pointer to the CircularBuffer, which is not a char.

If you need a pointer to an element you can use

char c = chars[0];
pGet = &c;

If you intend to get access to the internal structure that is protected from external access: being a circular buffer there is no guarantee your first item precedes the second item in the internal structure...

rlogiacco commented 6 years ago

By re-reading the title of the question I believe there's a misunderstanding: a circular buffer is a data structure, like an array or a linked list, but it's not an array and you cannot "cast" the circular buffer to an array. It internally uses an array, but that's an implementation detail which this library tries to hide as much as possible: otherwise why use the library... You can create an array with the content of the circular buffer though, by for-looping over the buffer and populating a properly sized array: I wonder if the circular buffer is the proper data structure in this case though