Hello. I got a problem of that type:
for example i have a simple program like this:

int main(){
sleep(10);
}

now if i define time to be 10 sec, prog works fine.

#define TIME 10
sleep(TIME)

but i am trying to do something like that:

char time; // or int time;
time = "10";
sleep(time);

and it's not working. Prog sleeps untill i finish it myself manualy.
The same thing i am trying to do in simple socket app to connect the net.
This line:

dest_addr.sin_port = htons(DEST_PORT);

when DEST_PORT is defined, it's ok. But i'm trying to rebuild this like with that app with sleep. The errors i've got are:
passing argument 1 of 'htons' makes integer from pointer without a cast
or
assignment makes integer from pointer without a cast.
Please help me how to do it...

Dani AI

Generated

The real problem is that "10" is text (a pointer to characters), not a numeric value. Passing a pointer where an integer is expected produces the compiler warnings you saw and undefined behavior at runtime. was right to suggest converting the string to a number, and correctly pointed out the type mismatch — also avoid naming a variable time (it clashes with the standard time() symbol); use secs, timeout or port_str instead.

A robust conversion pattern (checks for non-digits and overflow) — convert a seconds string and use it with sleep:

#include <stdlib.h>
#include <errno.h>
#include <limits.h>

const char *s = "10";
char *end;
errno = 0;
unsigned long v = strtoul(s, &end, 10);
if (errno == 0 && end != s && *end == '\0' && v <= UINT_MAX) {
    unsigned secs = (unsigned)v;
    sleep(secs); /* POSIX: seconds */
} else {
    /* handle invalid input */
}

Same idea for a network port: parse, validate 0–65535, then store with htons using a fixed-width cast:

const char *port_s = "12345";
char *end;
unsigned long p = strtoul(port_s, &end, 10);
if (end != port_s && *end == '\0' && p <= 65535) {
    struct sockaddr_in addr;
    addr.sin_port = htons((uint16_t)p);
} else {
    /* bad port */
}

Notes: prefer strtoul/strtol over atoi because they report errors; check errno and endptr; use uint16_t for ports and widen/cast safely for htons. On Windows, use Sleep() (milliseconds) and initialize Winsock as needed; include the proper headers (<stdlib.h>, <errno.h>, <limits.h>, <stdint.h>, <arpa/inet.h> or <winsock2.h>).

Recommended Answers

All 3 Replies

Numbers and strings are two different kinds of thing.

You'll need to convert between them. Use strtol() or strtoul() to turn your string into a number (long or unsigned long respectively).

Hope this helps.

char time; // or int time;
time = "10";
sleep(time);

This is the prototype of sleep: unsigned sleep(unsigned seconds); Meaning it accepts an unsigned type argument and returns an unsigned type int time = 10; will work correctly as long as time is an unsigned integer. time = "10"; is not an integer. It is a string, an incorrect declared string, but a string nevertheless. #define TIME 10 TIME is a constant of an unsigned integer. Therefore it woks.

Duoas, thank you! Problem solved. Yes, i was about how to convert number to string, just didnt know how to express myself properly. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.