0

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

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by somename
0

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.

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.