0

Can anybody explain why the following code gives the following warning and how to remedy it?

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

Here is the code:

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 Answer 1

8

Correct this:

socket = tcp_socket;

to

socket = tcp_socket();

tcp_socket() is a function call, while tcp_socket is a pointer to the function location in memory.

Sign up to request clarification or add additional context in comments.

3 Comments

@Tomcelic : Based on your reputation, I think either you were in hurry, or frustrated (of long hours of coding) to do this kind of mistake. :-)
I'm actually newish to C, she is a cold and cruel mistress !
@Tomcelic Be patient with her and one day she will take you to the wonderland :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.