title: Misunderstanding something when passing by reference (C structures)
body: <p>i'm doing some basic C coding and running into an issue that i can't figure out. So my current setup is that i have delcared a simple structure:</p>
  <pre><code>struct UserInfo
  {
     WCHAR* UserName;
     WCHAR* ComputerName;
  }
  </code></pre>
  <p>And from my main I want to create an instance of that structure, pass the structure by reference to two functions each of which will populate one of the two fields. my main code is:</p>
  <pre><code>void main()
  {
     struct UserInfo userInfo;
  
     GetUserName(&amp;userInfo);
     GetComputerName(&amp;userInfo);
  
     return 0;
  }
  </code></pre>
  <p>The two functions are as follows:</p>
  <pre><code>BOOL GetUserName(struct UserInfo* userInfo)
  /*
  Function calls the winapi function GetUserNameW to return the current
  username in widechar format.
  */
  {
      WCHAR UserName[USERNAME_MAXSIZE + 1];
      DWORD UserNameSize = USERNAME_MAXSIZE + 1;
  
      if (!GetUserNameW(UserName, &amp;UserNameSize))
      {
          if (&amp;UserNameSize != USERNAME_MAXSIZE + 1)
          {
              if (!GetUserNameW(UserName, &amp;UserNameSize)) return FALSE;
          }
          else return FALSE;
      }
      userInfo-&gt;UserName = UserName;
  
      return TRUE;
  }
  </code></pre>
  <p>and</p>
  <pre><code>BOOL GetComputerName(struct UserInfo* userInfo)
  {
      LPWSTR ComputerBuffer[MAX_PATH];
      DWORD ComputerBufferSize = MAX_PATH;
      if (!GetComputerNameW(ComputerBuffer, &amp;ComputerBufferSize))
      {
          if (&amp;ComputerBufferSize != MAX_PATH)
          {
              if (!GetComputerNameW(ComputerBuffer, &amp;ComputerBufferSize)) return FALSE;
          }
          else return FALSE;
      }
  
      userInfo-&gt;ComputerName = ComputerBuffer;
  
      return TRUE;
  }
  </code></pre>
  <p>The issue i have is that it seems when the second function is called the data stored in the first value is overwritten. So for instance when going through the code the first function populates userInfo-&gt;UserName correctly with a pointer to my systems username.</p>
  <p>When the second function is called user userInfo-&gt;UserName value seems to be overwritten, the second function runs fine and the userInfo-&gt;ComputerName is correctly populated. I'm confused to why this is happening? If i merge these functions into one like so:</p>
  <pre><code>BOOL GetData(struct UserInfo* userInfo)
  {
      WCHAR UserName[USERNAME_MAXSIZE + 1];
      DWORD UserNameSize = USERNAME_MAXSIZE + 1;
      LPWSTR ComputerBuffer[MAX_PATH];
      DWORD ComputerBufferSize = MAX_PATH;
  
      if (!GetUserNameW(UserName, &amp;UserNameSize))
      {
          if (&amp;UserNameSize != USERNAME_MAXSIZE + 1)
          {
              if (!GetUserNameW(UserName, &amp;UserNameSize)) return FALSE;
          }
          else return FALSE;
      }
      userInfo-&gt;UserName = UserName;
  
      if (!GetComputerNameW(ComputerBuffer, &amp;ComputerBufferSize))
      {
          if (&amp;ComputerBufferSize != MAX_PATH)
          {
              if (!GetComputerNameW(ComputerBuffer, &amp;ComputerBufferSize)) return FALSE;
          }
          else return FALSE;
      }
  
      userInfo-&gt;ComputerName = ComputerBuffer;
  
      return TRUE;
  }
  </code></pre>
  <p>This all works as expected, am i missunderstanding something about passing by reference? From my understanding &amp;userInfo is basically passing passing a pointer to my structure and userInfo-&gt;somefield is actually modifying my structure not a copy structure as would be the case with passing by value. I don't understand why when passing the same struct to different functions each modifying a different field they are interfearing with eachother?</p>
  <p>Thanks in advance for any help understanding this.</p>