How can I read the total number of the bytes written into a larger array? For example if 10 bytes are written in a uint8_t array[100], how can I check if the written bytes are 10 and not 20 or 80 or 100

Since sizeof(array) would give us the total number of the array (100 bytes), we cannot know how many bytes are actually written in the array and how many array cells are empty.


closed as off-topic by DoxyLover, Lundin, Voltage Spike, Dmitry Grigoryev, JYelton Jul 19 '18 at 22:08

  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ C strings show one approach. \$\endgroup\$ – Brian Drummond Jun 27 '18 at 15:01
  • 1
    \$\begingroup\$ Note that your array will likely NOT have "nothing" in it. It will exist in a place in memory and probably have random bits - that is unless you initialize it. \$\endgroup\$ – mike65535 Jun 27 '18 at 15:02
  • 1
    \$\begingroup\$ C doesn't work that way. If you're programming in C you need to learn to tie your own shoelaces because it's not going to do it for you. The array elements exist and are not empty whether you've written something to them or not. \$\endgroup\$ – brhans Jun 27 '18 at 15:06
  • \$\begingroup\$ Dear brhans, yes I understand that C cannot recognize array elements as empty, I just said it that way to give you understand what I was thinking. \$\endgroup\$ – Sparky Jun 27 '18 at 15:39
  • 2
    \$\begingroup\$ I'm voting to close this question as off-topic because this is a pure software question with no real association to electronics. It belongs on stackoverflow. \$\endgroup\$ – DoxyLover Jun 27 '18 at 19:46

You need to keep track of that yourself.

For example, if you're using the array as a circular buffer, you would have additional variables that hold the index values for the "head" (newest) and "tail" (oldest) valid values. You advance the "head" index whenever you add a byte, and advance the "tail" whenever you remove one.

The difference between the "head" and "tail" indices (modulo the size of the array) tells you how many bytes are valid.


Not the answer you're looking for? Browse other questions tagged or ask your own question.