C: incompatible types in assignment

Posted by The.Anti.9 on Stack Overflow See other posts from Stack Overflow or by The.Anti.9
Published on 2010-04-07T04:23:34Z Indexed on 2010/04/07 4:33 UTC
Read the original article Hit count: 233

Filed under:

I'm writing a program to check to see if a port is open in C. One line in particular copies one of the arguments to a char array. However, when I try to compile, it says:

error: incompatible types in assignment

Heres the code. The error is on the assignment of addr

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
  u_short port;                /* user specified port number */
  char addr[1023];             /* will be a copy of the address entered by u */
  struct sockaddr_in address;  /* the libc network address data structure */
  short int sock = -1;         /* file descriptor for the network socket */

  port = atoi(argv[1]);
  addr = strncpy(addr, argv[2], 1023);
  bzero((char *)&address, sizeof(address));  /* init addr struct */
  address.sin_addr.s_addr = inet_addr(addr); /* assign the address */
  address.sin_port = htons(port);            /* translate int2port num */

  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(sock,(struct sockaddr *)&address,sizeof(address)) == 0) {
    printf("%i is open\n", port);
  }
  if (errno == 113) {
    fprintf(stderr, "Port not open!\n");
  }
  close(sock);
  return 0;
}

I'm new to C, so I'm not sure why it would do this.

© Stack Overflow or respective owner

Related posts about c