I am having trouble figuring how to make a modification to a piece of code for a project I am working on.
The original code is
class foo {
public:
...
private:
uint8_t _size;
uint8_t *address;
uint8_t *rx_buffer;
}
// And down in a method a call is made to
// Copy the received data into address
memcpy(address, rx_buffer, _size);
And everything works well.
I want to modify it so I can handle multiple addresses so I made the following changes
class foo {
public:
...
private:
uint8_t _size[3]; //size of each struct
uint8_t _key[3]; // the key to identify each struct
uint8_t _index; // the index of the last struct added
uint8_t *address[3]; //address of each struct
uint8_t *rx_buffer; //address for temporary storage and parsing
}
// Copy received data into address
memcpy(address[index],rx_buffer,_size[index]);
No longer works!
As you can tell, I am not yet a C++ programmer. I am trying to learn with my projects. If someone would be kind enough to tell me how to think about it, I would be much appreciative.
I think the way you wrote it you have a single pointer to an array containing three bytes. What you want is an array of three pointers pointing to bytes.
in the example index is a passed in variable to point to one of the addresses. _index is the variable that is used to manage the adding of pointers and how many there are. I don't think it would be a good idea to put all of the code out, as it will just make it more difficult to understand where I am stumped.
Well, I think we need to see more. The current pieces look OK to me.
What does "no longer works" mean, exactly, anyway? Doesn't compile? compiles but hangs when run? Compiles and seems to run but doesn't do the right thing?
Just a quick note: There is an ancient C utility (that's been upgraded for C with crutches) called cdecl. I know that source is freely downloadable from a number of web sites for *nix systems, but I'm not sure if there's a Windows version (if not, it's available as a web resource at sites like http://www.cdecl.org/. Cdecl does 2-way translations between declarations and English. I've found it handy both for unraveling complicated declarations and for coding declarations I wasn't sure how to write.
It translates unsigned char *address[3] to "declare address as array 3 of pointer to unsigned char".