C: incompatible types in assignment

前端 未结 4 1410
感情败类
感情败类 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:59

    The line

    addr = strncpy(addr, argv[2], 1023);
    

    should be just

    strncpy(addr, argv[2], 1023);
    

    note that strncpy doesn't null-terminate if the 1023 limit is reached so you should also have

    addr[1023] = `\0`;
    

    though I suppose there's other assumptions made in the code as well.

提交回复
热议问题