C: incompatible types in assignment

前端 未结 4 1415
感情败类
感情败类 2021-01-27 14:02

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:

4条回答
  •  春和景丽
    2021-01-27 14:54

    an alternative is to use a char * instead:

    char *addr;
    
    addr = strdup(argv[2]);
    

    strdup is basically a shortcut that does a malloc and a strcpy, and you don't have to worry about the size of addr upfront. Don't forget to free addr once you're done.
    Note that if argv[2] is NULL you will get a segfault.

提交回复
热议问题