C: incompatible types in assignment

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

    addr is an array so you can't assign to it directly.

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

    A pointer to what you passed in is returned, but this value isn't needed. The call to strncpy alone will copy the string from argv[2] to addr.


    Note: I notice sometimes you pass in the address of your array and sometimes you pass in the array itself without the address of operator.

    When the parameter only asks for char*...

    Although both will work passing in addr instead of &addr is more correct. &addr gives a pointer to a char array char (*)[1023] whereas addr gives you a char* which is the address of the first element. It usually doesn't matter but if you do pointer arithmetic then it will make a big difference.

提交回复
热议问题