title: Data encapsulation in C
body: <p>I am currently working on an embedded system and I have a component on a board which appears two times. I would like to have one .c and one .h file for the component.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>typedef struct {
      uint32_t pin_reset;
      uint32_t pin_drdy;
      uint32_t pin_start;
      volatile avr32_spi_t *spi_module;
      uint8_t cs_id;  
  } ads1248_options_t;
  </code></pre>
  
  <p>Those are all hardware settings. I create two instances of this struct (one for each part).</p>
  
  <p>Now I need to keep an array of values in the background. E.g. I can read values from that device every second and I want to keep the last 100 values. I would like this data to be non-accessible from the "outside" of my component (only through special functions in my component).</p>
  
  <p>I am unsure on how to proceed here. Do I really need to make the array part of my struct? What I thought of would be to do the following:</p>
  
  <pre><code>int32_t *adc_values; // &lt;-- Add this to struct
  
  int32_t *adc_value_buffer = malloc(sizeof(int32_t) * 100); // &lt;-- Call in initialize function, this will never be freed on purpose
  </code></pre>
  
  <p>Yet, I will then be able to access my int32_t pointer from everywhere in my code (also from outside my component) which I do not like.</p>
  
  <p>Is this the only way to do it? Do you know of a better way?</p>
  
  <p>Thanks.</p>