title: Setting one member variable of a struct seems to be altering another
body: <p>This is for a class on Compilers.</p>
  
  <p>I'm calling this code:</p>
  
  <pre><code>struct attribute attr = {.token = token, .buffer = *buffer, .length = length, .format = format};
  fprintf(stdout, "after struct: %d\n", data.flags);
  data.attributes[i] = attr;
  fprintf(stdout, "after data.attributes[i] = attr: %d\n", data.flags);
  </code></pre>
  
  <p>from a function with this prototype:</p>
  
  <pre><code>int attribute( int token, char *buffer, unsigned int length, int format);
  </code></pre>
  
  <p>and it goes to this struct:</p>
  
  <pre><code>#define DATA    struct data
  DATA
  {
  //define the scanner attribute table
  #define MAX_ATTRIBUTES  128
      ATTRIBUTE attributes[MAX_ATTRIBUTES];
      unsigned int index;
      int column;
      int flags;
  #define FLAGS_ECHO      0x0001
  #define FLAGS_DEBUG     0x0002
  #define FLAGS_PARSE     0x0004
  #define FLAGS_SYMBOL    0x0008
  #define IS_FLAGS_ECHO(a)    (a &amp; FLAGS_ECHO)    
  #define SET_FLAGS_ECHO(a)   (a |= FLAGS_ECHO)
  #define CLR_FLAGS_ECHO(a)   (a &amp;= ~FLAGS_ECHO)
  #define IS_FLAGS_DEBUG(a)   (a &amp; FLAGS_DEBUG)   
  #define SET_FLAGS_DEBUG(a)  (a |= FLAGS_DEBUG)
  #define CLR_FLAGS_DEBUG(a)  (a &amp;= ~FLAGS_DEBUG)
  #define IS_FLAGS_PARSE(a)   (a &amp; FLAGS_PARSE)   
  #define SET_FLAGS_PARSE(a)  (a |= FLAGS_PARSE)
  #define CLR_FLAGS_PARSE(a)  (a &amp;= ~FLAGS_PARSE)
  #define IS_FLAGS_SYMBOL(a)  (a &amp; FLAGS_SYMBOL)
  #define SET_FLAGS_SYMBOL(a) (a |= FLAGS_SYMBOL)
  #define CLR_FLAGS_SYMBOL(a) (a &amp;= ~FLAGS_SYMBOL)
  };
  </code></pre>
  
  <p>The ATTRIBUTE struct is here:</p>
  
  <pre><code>#define ATTRIBUTE       struct attribute
  ATTRIBUTE{
      int token;
  #define MAX_BUFFER_SIZE     128
      char buffer[MAX_BUFFER_SIZE];
      int length;
  #define FORMAT_NONE         0
  #define FORMAT_CHAR         1
  #define FORMAT_DECIMAL      2
  #define FORMAT_HEXIDECIMAL  3
  #define FORMAT_OCTAL        4
  #define FORMAT_FLOAT        5
      int format;
  };
  </code></pre>
  
  <p>The problem is that when I call that first bit of code the first time, the value of <code>data.flags</code> gets reset from 8 to 0.</p>
  
  <p>All the formatting and (at least seemingly to me) excessive use of macros are per the professor, so I didn't change them.</p>
  
  <p>Does anyone know why this would be happening? Please let me know if you need more information than I've given.</p>
  
  <p>EDIT:</p>
  
  <p>It seems that the problem is likely coming from where I determine <code>i</code>:</p>
  
  <pre><code>for(i = sizeof(data.attributes)/sizeof(data.attributes[0]) - 1; i &gt;= 0; i--){
      if(&amp;(data.attributes[i]) != NULL) break;
  }
  </code></pre>
  
  <p>Question Answered Edit:</p>
  
  <p>Thanks to the information gained from Zan Lynx's answer, I figured out that rather than the way I had been doing it, I needed to initialize the elements of the attributes array somewhere like this:</p>
  
  <pre><code>int i;
  struct attribute null_attr = {.token = -1, .format = -1, .length = -1};
  for(i = 0; i &lt; sizeof(data.attributes)/sizeof(data.attributes[0]); i++){
      data.attributes[i] = null_attr;
  }
  </code></pre>
  
  <p>and then check for the earliest uninitialized attribute like this:</p>
  
  <pre><code>for(i = sizeof(data.attributes)/sizeof(data.attributes[0]) - 1; i &gt;= 0; i--){
      if(data.attributes[i].token != -1
        &amp;&amp; data.attributes[i].length != -1
        &amp;&amp; data.attributes[i].format != -1)
      break;
  }
  </code></pre>