赋值从指针生成整数而没有强制转换警告

时间:2013-09-10 12:03:28

标签: c pointers casting variable-assignment

有人可以解释为什么以下代码会发出以下警告以及如何解决它?

warning: assignment makes integer from pointer without a cast [enabled by default]

以下是代码:

int tcp_socket(void)
{
    int s;

    if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        _logf(LOG_DEBUG, "LOG %s:%d (%s) - %s", __FILE__, __LINE__, __func__, "Error creating socket");
        return -1;
    }
    return s;       
}

int main(int argc, char **argv)
{
    int socket;
    socket = tcp_socket;
    if(socket == -1) {
        _logf(LOG_INFO, "LOG %s:%d (%s) - %s", __FILE__, __LINE__, __func__, "Error creating socket, exiting...");
        exit(-1);
    } 
}

1 个答案:

答案 0 :(得分:8)

纠正这个:

socket = tcp_socket;

socket = tcp_socket();

tcp_socket()是函数调用,而tcp_socket是指向内存中函数位置的指针。