Re: Whats the meaning of this code



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.

.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: Question about void pointers
    ... That is undefined behavior by reason of trying to print an pointer ... unsigned int on many systems. ... Yes, but that difference has no portable meaning, and the ...
    (comp.lang.c)