Re: Whats the meaning of this code
- From: "santosh" <santosh.k83@xxxxxxxxx>
- Date: 24 Jan 2007 10:10:51 -0800
Harald van Dijk wrote:
santosh wrote:
Noah Roberts wrote:
On Jan 24, 8:34 am, "sam" <samsoft...@xxxxxxxxx> wrote:
HI,
The code is as follows:-
#include <stdio.h>
1
2 void * work(void * ptr)
3 {
4 int i;
5 for (i = 0; i < 10; i++)
6 {
7 printf("%d", (int)ptr);
8 usleep(1000);
9 }
10 return 0;
11 }
Whats the meaning of this line:-
void *work(void *ptr)
Though this is valid C++ the techniques are used more often in C. The
use of the stdio.h header also indicates C. If you're coding in C
you'll want to go to that newsgroup. We'll tell you not to write code
like that.
The meaning...a function called work that accepts a pointer of type
void (basically meaninng "whatever") and returns the same.
Note though that the printf invokes undefined behaviour, since the
pointer value is cast to an int value.
It is allowed (but generally not a good idea) to convert pointers to
integers, and then to print them as integers. The behaviour is
undefined if the pointer-as-integer is outside of the range of the
integer type, but the value of the pointer-as-integer is
implementation-defined, so the programmer can know in advance whether
int will be large enough for a specific implementation.
Thanks. I must take care to follow the standard more closely in future.
.
- References:
- Re: Whats the meaning of this code
- From: santosh
- Re: Whats the meaning of this code
- From: Harald van Dijk
- Re: Whats the meaning of this code
- Prev by Date: Re: checking if pointer is NULL allowed?
- Next by Date: Re: checking if pointer is NULL allowed?
- Previous by thread: Re: Whats the meaning of this code
- Next by thread: weird problem
- Index(es):
Relevant Pages
|