title: inet_pton() gives "Segmentation fault"
body: <p>In my program, I am using the <code>inet_pton</code> function one of two ways:</p>
  
  <p>First: Default IP if none is specified in argument.</p>
  
  <pre><code>inet_pton(AF_INET, defaultIP, &amp;servaddr.sin_addr)
  </code></pre>
  
  <p>Second: When a command line IP address is given.</p>
  
  <pre><code>inet_pton(AF_INET, argv[1], &amp;servaddr.sin_addr)
  </code></pre>
  
  <p>The problem is, the first one gives me a Segmentation fault, and the second one does not. I have a feeling it has to do with how I pass in the default IP but I can not quite figure it out. I defined defaultIP like this:</p>
  
  <pre><code>const char *defaultIP = "127.0.0.1";
  </code></pre>
  
  <p>I have tried many variations of passing in the IP address with no success. Perhaps this is not even the issue? However, if I pass in 127.0.0.1 at the command line, It works fine passing in through argv. Thanks in advance for any replies!</p>
  
  <p>The entirety of the code I am working with is now here, as requested.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  
  #define SA struct sockaddr
  #define MAXLINE     4096
  
  void err_sys(const char*);
  
  int
  main(int argc, char **argv)
  {   
      const char *defaultIP = "127.0.0.1";
      int                 sockfd, n;
      char                recvline[MAXLINE + 1];
      struct sockaddr_in  servaddr;
      char defaultAddr = 0;
  
      if (argc != 2)
          defaultAddr = 1;
  
      if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          err_sys("socket error");
  
      memset(&amp;servaddr, 0, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_port   = htons(13);    /* daytime server */
  
      if (defaultAddr = 1)
          if (inet_pton(AF_INET, defaultIP, &amp;servaddr.sin_addr) &lt;= 0)
              err_sys("inet_pton error for input IP address");
      else
          if (inet_pton(AF_INET, argv[1], &amp;servaddr.sin_addr) &lt;= 0)
              err_sys("inet_pton error for input IP address");
  
      if (connect(sockfd, (SA *) &amp;servaddr, sizeof(servaddr)) &lt; 0)
          err_sys("connect error");
  
      while ( (n = read(sockfd, recvline, MAXLINE)) &gt; 0) {
          recvline[n] = 0;    /* null terminate */
          if (fputs(recvline, stdout) == EOF)
              err_sys("fputs error");
      }
      if (n &lt; 0)
          err_sys("read error");
  
      exit(0);
  }
  
  void err_sys(const char* x) 
  { 
      perror(x); 
      exit(1); 
  }
  </code></pre>