GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jul-2009, 12:27
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 156
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about
Lightbulb

Function returning pointer to itself


Hello,

I was reading this famous C faq by steve summit where it was said that it is impossible for a function to return a pointer which points to a function of the same type. Mindboggling ? betcha!

nonetheless it also suggested 'wrapping' a struct around such a function and then returning the struct would work here. So i tried with:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    mystruct *func(); // line 5
} mystruct;

int main()
{
    printf("address of func is %p,using func %p\n",&func(),func); //line 10
    printf("address of struct is %p\n",&mystruct); //line 11
    return 0;
}
and I get the following compiler errors on a GCC based system:
Code:
main.c:5: error: expected specifier-qualifier-list before ‘mystruct’ main.c: In function ‘main’: main.c:10: warning: implicit declaration of function ‘func’ main.c:10: error: lvalue required as unary ‘&’ operand main.c:11: error: expected expression before ‘mystruct’ make: *** [main.o] Error 1
Then I tried with this as the solution alternatively suggested typecasting. My solution doesn't use typecasting but I thought would work anyways but it doesn't:
CPP / C++ / C Code:
void func();
func *func1();

int main()
{
    return 0;
}
and ended up with:
Code:
main.c:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token make: *** [main.o] Error 1
A slight modification including typecasting yields:
CPP / C++ / C Code:
void func();
func (func*)*func1();

int main()
{
    return 0;
}

And the results are:
Code:
main.c:9: error: expected ‘)’ before ‘*’ token make: *** [main.o] Error 1

I do not know how to use a typecast or a struct to create such a function. Input would be appreciated
__________________
Hope to hear from you guys!

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

Best Regards,
Aijaz Baig.
Last edited by aijazbaig1 : 10-Jul-2009 at 12:37. Reason: adding more code
  #2  
Old 10-Jul-2009, 17:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: function returning pointer to itself


Quote:
Originally Posted by aijazbaig1
...
I was reading this famous C faq by steve summit

The thing about lists of Frequently Asked Questions is that they usually post some answers with a little insight thrown in: http://c-faq.com/decl/recurfuncp.html

For examples of declarations and uses of function pointers in the same FAQ, see: http://c-faq.com/decl/pfitypedef.html and http://c-faq.com/ptrs/funccall.html



Regards,

Dave
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 16:23
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:30.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.