Function Retruning Ptr to a Itself 
Author Message
 Function Retruning Ptr to a Itself

Can anyone rite a Function returning pointer to itself without using
void...

Regds,
Vinoth Kumar.A



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself


Quote:
> Can anyone rite a Function returning pointer to itself without using
> void...

> Regds,
> Vinoth Kumar.A

You must first ENUMERATE GROUNDS for your EVALUATION REQUEST.  Karl M,
chair.


Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

Quote:

> Can anyone rite a Function returning pointer to itself without using
> void...

> Regds,
> Vinoth Kumar.A

The following program displays the two lines

108f4
108f4

when compiled with egcs on my Solaris box (that is the address of the
function foo()):

#include <stdio.h>

int *foo() {
  return (int *) foo;

Quote:
}

int main() {
  int *ptr = (int *) foo;

  printf("%p\n", foo());
  printf("%p\n", ptr);

  return 0;

Quote:
}

A more correct solution (i.e. it doesn't cast the function pointer to a
int*) would be to write the function foo() as

void *foo() {
  return foo;

Quote:
}

...but we were not allowed to use 'void'.

/A

ps: There might be something fishy here... I will let the more
experience C gurus correct my errors without me objecting.

--
+---------------------+
| Andreas K?h?ri
| Uppsala University
| Sweden

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself


[cut]

Quote:
> You must first ENUMERATE GROUNDS for your EVALUATION REQUEST.  Karl M,
> chair.

Excuse me, but didn't I tell you there's NO HOPE for the survival of
OFFSET PRINTING?

/A

--
+---------------------+
| Andreas K?h?ri
| Uppsala University
| Sweden

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

Quote:



>> Can anyone rite a Function returning pointer to itself without using
>> void...

>> Regds,
>> Vinoth Kumar.A

>You must first ENUMERATE GROUNDS for your EVALUATION REQUEST.  Karl M,
>chair.

I think Karl wants you to explain further your question.  ;-)

--
Chris Mears


ICQ: 36697123

C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself
I don't think it's possible. If you can't use void, it's like
the chicken-egg problem.
-Engineer

Quote:
> Can anyone rite a Function returning pointer to itself without using
> void...

> Regds,
> Vinoth Kumar.A



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

Quote:

> Can anyone rite a Function returning pointer to itself without using
> void...

You mean incant a couple of spells so that it will magically appear ;-)?

Or did you mean something like this:
<http://www.eskimo.com/~scs/C-faq/q1.22.html>?

Richard



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

Quote:





> >> Can anyone rite a Function returning pointer to itself without using
> >> void...

> >> Regds,
> >> Vinoth Kumar.A

> >You must first ENUMERATE GROUNDS for your EVALUATION REQUEST.  Karl M,
> >chair.

> I think Karl wants you to explain further your question.  ;-)

I think Karl is a chair. I don't think further explanation of his posts
is necessary, giving that furnitural status <g>.

Richard



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself
Thanx a lot for the info...

karl: My friend has asked this Question to me...
        That is the reason i sent this problem to u all...

I gave him solution in this way

 void * IwillReturnMyPointer()
 {
  void * (*MyPtr)();
  printf("*******");
  MyPtr=IwillReturnMyPointer;
  return MyPtr;
 }

But he told me that i should not use void pointer.

I tried to find a solution...But my attempts went in vein...

My sincere thanks to Chris,Richard,Andreas,Engineer

Regds,
Vinoth Kumar.A

Quote:

> Can anyone rite a Function returning pointer to itself without using
> void...

> Regds,
> Vinoth Kumar.A



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself


Quote:


>> Can anyone rite a Function returning pointer to itself without using
>> void...

>> Regds,
>> Vinoth Kumar.A

>The following program displays the two lines

>108f4
>108f4

>when compiled with egcs on my Solaris box (that is the address of the
>function foo()):

>#include <stdio.h>

>int *foo() {
>  return (int *) foo;
>}

However casting a function pointer to a data pointer results in
undefined behaviour. void * is a data pointer so even if you could use
it it wouldn't help. You can cast between different pointer to function
types (as long as you cast back before calling the function) so the
following works:

#include <stdio.h>

typedef void (*Genfuncptr)(void);

static Genfuncptr foo(void);

int main(void)
{
    Genfuncptr funcp;

    funcp = foo();
    funcp = ((Genfuncptr (*)(void))funcp)();
    return 0;

Quote:
}

static Genfuncptr foo(void)
{
    printf("foo called\n");
    return (Genfuncptr)foo;

Quote:
}

You can do it without the typedef but it gets a little messy.

- Show quoted text -

Quote:
>int main() {
>  int *ptr = (int *) foo;

>  printf("%p\n", foo());
>  printf("%p\n", ptr);

>  return 0;
>}

>A more correct solution (i.e. it doesn't cast the function pointer to a
>int*) would be to write the function foo() as

>void *foo() {
>  return foo;
>}

>...but we were not allowed to use 'void'.

You *must* us a cast to convert between function pointer and void *.
However the result is just as undefined as converting to and from int *
so this is no more correct. Another approach that avoids the use of
casts is to use a structure:

#include <stdio.h>

typedef struct Genfuncptr {
    struct Genfuncptr (*fp)(void);

Quote:
} Genfuncptr;

static Genfuncptr foo(void);

int main(void)
{
    Genfuncptr funcp;

    funcp = foo();
    funcp = funcp.fp();
    return 0;

Quote:
}

static Genfuncptr foo(void)
{
    Genfuncptr funcp = { foo };

    printf("foo called\n");
    return funcp;

Quote:
}

--
-----------------------------------------


-----------------------------------------


Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

[ material rearranged to match reply order; please do it right next time ]

Quote:
>> Can anyone rite a Function returning pointer to itself without using
>> void...

>I don't think it's possible. If you can't use void, it's like
>the chicken-egg problem.
>-Engineer

You can leverage the incomplete struct type to emulate the effect.

That is, the function can return a struct which contains a pointer to that
function. That pointer can be written in terms of the incomplete struct type.

When the struct type is completed, it then all hangs together.

It's sort of like closing a cardboard box in a ``windmill'' arrangement,
whereby each flap overlaps the one to its right.  You have to bend the
cardboard a little bit to get it there!   The incomplete struct or union
declaration is the place  in the language where you get this bending.

    struct function_pointer {
        struct function_pointer (*fptr)(int);
    };

    struct function_pointer function(int arg)
    {
        struct function_pointer return_value = { function };
        return return_value;
    }

Pedantically speaking, the function doesn't actually return a pointer, but
rather a struct containing a pointer. The difference is academic; what counts
is that the desired effect is achieved.



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

Quote:

>A more correct solution (i.e. it doesn't cast the function pointer to a
>int*) would be to write the function foo() as

>void *foo() {
>  return foo;
>}

>...but we were not allowed to use 'void'.

That is not more correct at all. It's not even valid C.

You cannot implicitly convert a function pointer to void *, so there
is a constraint violation. Moreover, ANSI C does not support conversions
among void * and function pointers; the ability to do so is a local extension
to the language. (Has this changed in C99?)



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

karl malbrain a crit dans le message

Quote:



>> Can anyone rite a Function returning pointer to itself without using
>> void...

>> Regds,
>> Vinoth Kumar.A

>You must first ENUMERATE GROUNDS for your EVALUATION REQUEST.  Karl M,
>chair.

Once again you have forgotten your pills.

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Sun, 25 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself

Quote:

> The following program displays the two lines
> 108f4
> 108f4
> when compiled with egcs on my Solaris box (that is the address of the
> function foo()):
> #include <stdio.h>
> int *foo() {
>   return (int *) foo;
> }
> int main() {
>   int *ptr = (int *) foo;
>   printf("%p\n", foo());
>   printf("%p\n", ptr);
>   return 0;
> }

You were lucky.  Pointers to objects (and to void) and pointers to
functions are not guaranteed to be compatible types in C; obviously the
implementation you used has them compatible.

--
Eric Amick
Columbia, MD



Mon, 26 Aug 2002 03:00:00 GMT  
 Function Retruning Ptr to a Itself
On Wed, 8 Mar 2000 23:28:09 +0100, "-hs-"

Quote:

>karl malbrain a crit dans le message



>>> Can anyone rite a Function returning pointer to itself without using
>>> void...

>>> Regds,
>>> Vinoth Kumar.A

>>You must first ENUMERATE GROUNDS for your EVALUATION REQUEST.  Karl M,
>>chair.

>Once again you have forgotten your pills.

And he still thinks he's a chair.
        Mark M, bookcase.

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Mon, 26 Aug 2002 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. ptr to ptr to ptr..... n times

2. (member) function returning pointer to functions like itself?

3. function returning a function ptr

4. typedef the function or the function ptr?

5. Function that returns a function ptr

6. Function ptr with void* argument can't be assinged function with anytype* ar

7. Funct retng ptr to funct retng ptr to char

8. ptr to ptr question

9. ptr 2 ptr stuff

10. *ptr++ (*ptr)++

11. is it legal to free(ptr) where ptr=NULL

12. Ptr to Ptr problem.

 

 
Powered by phpBB® Forum Software