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...

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.