Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3786

Re: Passing an integer to a function that accepts a void pointer

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: Passing an integer to a function that accepts a void pointer
Date 2011-05-11 22:10 -0500
Organization A noiseless patient Spider
Message-ID <iqfj33$dnr$1@dont-email.me> (permalink)
References <80905e4d-6b11-4843-b4e7-96f0c7db8b23@l18g2000yql.googlegroups.com> <4b5e6c3e-0266-44e4-8f29-dd46a91eeb40@x6g2000yqj.googlegroups.com> <iqedfc$77o$1@nnrp.ngi.it>

Show all headers | View raw


On 5/11/2011 11:28 AM, pozz wrote:
> Il 11/05/2011 12:52, Tom St Denis ha scritto:
>> Why not just do this
>>
>> int bar = 10;
>> foo(&bar);
>>
>> And inside foo do
>>
>> printf("%d\n", *((int *)Args));
>>
>> That way the program is actually portable.
>
> Oh yes, this is another solution I considered and surely it is the most
> portable way.
>
> In my case, I have a long list of const struct initialized with callback
> and argument pointers, like this:
>...

In your example code, you are using the same call-back for every 
different number.  If you are going to be using the same call-back, is 
there a reason why you cannot pass a 'void *' context that points 
directly to an element of an array of 'int'?  As in:
---
   #include <stdlib.h>
   #include <stdio.h>

   /*** Constants */
   /* None */

   /*** Object type declarations */
   typedef unsigned char byte;

   /*** Function types */
   typedef int f_callback(const void *);
   typedef int f_intfunc(int);

   /*** Structure type definitions */
   /* None */

   /*** Function declarations */
   static f_callback my_callback;
   static f_intfunc some_func;

   /* This one doesn't belong to you and comes from a header */
   extern int SomeApiPerformCallback(f_callback *, const void *);

   /*** Objects */
   static int my_ints[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

   /*** Function definitions */

   /**
    * This callback will call 'some_func' with the 'int' value
    * context that is associated with it.
    */
   static int my_callback(const void * context) {
       const int * int_ptr;

       int_ptr = context;
       if (!int_ptr)
         return -1;

       printf("my_callback() context: %p\n", context);
       return some_func(*int_ptr);
     }

   /**
    * This quite simply returns the 'int' that is passed
    */
   static int some_func(int int_param) {
       printf("some_func() got: %d\n", int_param);
       return int_param;
     }

   /**
    * The program entry-point
    */
   int main(void) {
       int result;

       result = SomeApiPerformCallback(my_callback, my_ints + 5);
       if (result == -1)
         return EXIT_FAILURE;
       printf("Result: %d\n", result);

       result = SomeApiPerformCallback(my_callback, my_ints + 7);
       if (result == -1)
         return EXIT_FAILURE;
       printf("Result: %d\n", result);

       result = SomeApiPerformCallback(my_callback, my_ints + 3);
       if (result == -1)
         return EXIT_FAILURE;
       printf("Result: %d\n", result);

       return EXIT_SUCCESS;
     }

   /* This one doesn't belong to you and is just a demo */
   int SomeApiPerformCallback(f_callback * callback, const void * context) {
       if (!callback)
         return -1;

       return callback(context);
     }

---

I would expect that unlike this simple code, you are registering a 
call-back which will be executed asynchronously relative to the function 
which registers it.  In that case, unless you wish to wait for the 
call-back to complete, I can understand why you wouldn't want to do:

   void blah(void) {
       int i;

       /* ... */
       i = 5;
       SomeApiRegisterCallback(my_callback, &i);
       return;
     }

because the lifetime of 'i' will end pretty well immediately after 
you've registered your call-back. :)  But...  That's what malloc() and 
the other memory allocation functions are for...

   void blah(void) {
       int *i;

       i = malloc(sizeof *i);
       if (!i) {
           /* Note an error */
           return;
         }

       *i = 5;
       SomeApiRegisterCallback(my_callback, i);
       return;
     }

Remember to free() the pointer once you've finished with it. :)

Any pointer type which points to a complete or incomplete object type 
can be converted to 'void *' (and back again), if I recall correctly. 
So that includes 'int *' -->> 'void *'.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-11 03:01 -0700
  Re: Passing an integer to a function that accepts a void pointer Tom St Denis <tom@iahu.ca> - 2011-05-11 03:52 -0700
    Re: Passing an integer to a function that accepts a void pointer pete <pfiland@mindspring.com> - 2011-05-11 08:10 -0400
    Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-11 18:28 +0200
      Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-11 22:10 -0500
        Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-12 19:44 +0200
          Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 14:16 -0400
            Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-12 21:21 +0200
              Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 16:05 -0400
                Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-13 23:39 +0200
                Re: Passing an integer to a function that accepts a void pointer Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-14 09:41 +0100
          Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 22:07 -0400
            Re: Passing an integer to a function that accepts a void pointer pozz <pozzugno@gmail.com> - 2011-05-13 23:12 +0200
              Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-13 23:30 -0400
  Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-11 04:03 -0700
    Re: Passing an integer to a function that accepts a void pointer Heikki Kallasjoki <see@end.of.message.invalid> - 2011-05-11 13:03 +0000
      Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-11 06:46 -0700
        Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-11 08:24 -0700
          Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-11 08:44 -0700
            Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-11 08:58 -0700
              Re: Passing an integer to a function that accepts a void pointer gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-11 17:38 +0000
                Re: Passing an integer to a function that accepts a void pointer Kenneth Brody <kenbrody@spamcop.net> - 2011-05-11 14:57 -0400
                Re: Passing an integer to a function that accepts a void pointer Paul N <gw7rib@aol.com> - 2011-05-11 14:06 -0700
                Re: Passing an integer to a function that accepts a void pointer Michael Press <rubrum@pacbell.net> - 2011-05-12 13:59 -0700
                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 14:12 -0700
                Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 15:27 -0700
                Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-12 22:37 +0000
                Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-13 10:41 +1200
                Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 17:57 -0500
                Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-13 11:05 +1200
                Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-12 23:19 +0000
                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 16:32 -0700
                Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 19:13 -0500
                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 18:18 -0700
                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 16:18 -0700
                Re: Passing an integer to a function that accepts a void pointer Tobias Blass <tobiasblass@gmx.net> - 2011-05-13 08:24 +0000
                Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-13 01:33 -0700
                Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-13 08:36 +0000
                Re: Passing an integer to a function that accepts a void pointer "madamemandm@yahoo.com" <madamemandm@yahoo.com> - 2011-05-13 07:08 -0700
                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 15:59 -0700
            Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-11 21:46 +0000
              Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 00:56 -0700
                Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-12 20:06 +1200
                Re: Passing an integer to a function that accepts a void pointer Heikki Kallasjoki <see@end.of.message.invalid> - 2011-05-12 08:59 +0000
                Re: Passing an integer to a function that accepts a void pointer Ian Collins <ian-news@hotmail.com> - 2011-05-13 10:39 +1200
        Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 06:51 -0400
          Re: Passing an integer to a function that accepts a void pointer Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 04:08 -0700
            Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 07:33 -0400
            Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 07:58 -0700
              Re: Passing an integer to a function that accepts a void pointer gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-12 15:23 +0000
              Re: Passing an integer to a function that accepts a void pointer Shao Miller <sha0.miller@gmail.com> - 2011-05-12 13:36 -0400
                Re: Passing an integer to a function that accepts a void pointer Keith Thompson <kst-u@mib.org> - 2011-05-12 12:03 -0700
            Re: Passing an integer to a function that accepts a void pointer Seebs <usenet-nospam@seebs.net> - 2011-05-12 15:57 +0000
          Re: Passing an integer to a function that accepts a void pointer Michael Press <rubrum@pacbell.net> - 2011-05-12 14:14 -0700
            Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 22:20 -0400
              Re: Passing an integer to a function that accepts a void pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-14 02:39 +0100
  Re: Passing an integer to a function that accepts a void pointer Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-11 07:37 -0400
  Re: Passing an integer to a function that accepts a void pointer James Kuyper <jameskuyper@verizon.net> - 2011-05-11 09:42 -0400
  Re: Passing an integer to a function that accepts a void pointer Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-11 11:53 -0400
    Re: Passing an integer to a function that accepts a void pointer Fred <fred.l.kleinschmidt@boeing.com> - 2011-05-11 10:44 -0700
      Re: Passing an integer to a function that accepts a void pointer Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-12 07:58 -0400
    Re: Passing an integer to a function that accepts a void pointer Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-11 21:27 +0100

csiph-web