title: Declaring variable with same name but different type
body: <p>I am trying to make something with supports IPV4 or IPV6.
  The code is similar for these two so I tried something like this.
  Since the only difference in code later on depends on this sin variable how can I compile just one of them. Lets suppose "ipv" variable in IF statement is true or false depends on user input</p>
  
  <pre><code>//FOR IPV4
  //struct sockaddr_in sin;
  //FOR IPV6
  //struct sockaddr_in6 sin;
  //IPV4
  
  
  // IPV4 ---------------------------------------
  if (ipv){
    struct sockaddr_in sin;   
    if ( (s = socket(AF_INET, SOCK_STREAM, 0 ) ) &lt; 0) { 
      perror("Chyba pri vytvareni socketu");  
      return -1;
    }
  
  
    sin.sin_family = AF_INET;     
    sin.sin_port = htons(port_number);
    sin.sin_addr.s_addr  = INADDR_ANY;
  }
  
  // IPV6 ---------------------------------------
  else{
  
    struct sockaddr_in6 sin;
    if ( (s = socket(AF_INET6, SOCK_STREAM, 0 ) ) &lt; 0) { 
      perror("Chyba pri vytvareni socketu");  
      return -1;
    }
  
    sin.sin6_family = AF_INET6;     
    sin.sin6_port = htons(port_number);
    sin.sin6_addr = in6addr_any;
    sin.sin6_flowinfo = 0;
  }   
  
  if (bind(s, (struct sockaddr *)&amp;sin, sizeof(sin) ) &lt; 0 ) {
      printf("error on bind\n"); return -1;  
  }
  
  if (listen(s, 5)) { 
      printf ("error on listen\n"); 
  return -1;
  }
  
  sinlen = sizeof(sin);
  pid_t pid;
  
  while (1) {
  
  
      /* accepting new connection request from client,
      socket id for the new connection is returned in t */
      if ( (t = accept(s, (struct sockaddr *) &amp;sin, &amp;sinlen) ) &lt; 0 ) {
        printf("error on accept\n");  /* accept error */
        return -1;
      }
  continues .... not important 
  </code></pre>
  
  <p>this code will give me:</p>
  
  <pre><code>server.cpp: In function ‘int main(int, char**)’:  
  server.cpp:132:35: error: ‘sin’ was not declared in this scope
     if (bind(s, (struct sockaddr *)&amp;sin, sizeof(sin) ) &lt; 0 ) {
                                 ^
  server.cpp:145:19: error: ‘sin’ was not declared in this scope
     sinlen = sizeof(sin);
                 ^
  make: *** [all] Error 1
  </code></pre>
  
  <p>So question is how can I make this done without writing the same code twice with different "sin";</p>