Repeating variable within struct

in an project , I am using some big struct that contains lots of variables, I'd like to keep it as one struct, because that simplified sending it when communicating
The project is heavy on RAM, som I need to save as much as possible.
a part of the struct is data that need to be sent, but is not unique
so instead of
struct {
....
uint8_t cell1;
uint8_t cell2;
uint8_t cell3;
...
}

I'd like to define it as:
struct {
....
uint8_t cells;
uint8_t cells;
uint8_t cells;
...
}

obviously, it's not that easy - what is the correct way ? I wish to save memory, so "cells" (uint8_t) should be saved/sent multiple times.

what is the correct way ?

An array.

I wish to save memory, so "cells" (uint8_t) should be saved/sent multiple times.

Take note of Paul's advice to use an array but if you store several values of cells then each one will take the same space whether you use arrays or multiple variables with different names.

Yes, array does not solve anything, this was a simplified example , and the goal is to save memory(RAM)
A messy solution would be to send first struct, then repeat variables using a loop, then send rest as struct, but I'd prefer a nice solution if possible.

Are the values such that you could use a bit mask to get to the values?

How do you 'communicate' the struct? Cast to a byte array? In that case I have no idea.

If you send the elements individually (no cast, struct.cell1, struct.cell2 etc), you can maybe use something like

struct {
....
  uint8_t cells;
  uint8_t repeat;
  ...
}

where repeat indicates how often cells needs to be repeated and use a loop while transmitting.

Just hope I understood the question correctly :wink:

sending like this:
memcpy(&outBuffer, &structdata, kstructdatasize);

  • this is why I wonld like to keep it simple, rather than add logic for processing in the middle of the struct.

You want to save memory but happily use memory for a copy of the struct :slight_smile: I don’t know your code but can’t you use a cast?

sterretje:
You want to save memory but happily use memory for a copy of the struct :slight_smile: I don't know your code but can't you use a cast?

yes, the bufferout is read asynchonously by request of another device, while this device collects, processes, and assemble a new struct. so can't use only one buffer, (in fact, there are several different struct's that are being prepared, then copied out to buffer and sent on the master device's request for one particualr type (closed/protected firmware in the master device.))