Search code examples
csocketsargv

How to take a argv and convert it to uint16_t


I need to take an argument (argv[2]) and convert it to uint16_t type. How I can get this? I put a PORT in argument so I need to convert in uint16_t, I tried this:

uint16_t PORT;

PORT = argv[2];

but when I do

servaddr.sin_port = htons(PORT) 

I get

warning: assignment makes integer from pointer without a cast

Solution

  • You can convert a char * to an int (and therefore, to a uint16_t) via the atoi() function:

    #include <stdlib.h>
    
    [...]
    
    uint16_t PORT = atoi(argv[2]);