-1

I have p like:

typedef int (*p)();

Let's say the function to which I want to declare a pointer is int foo(). I wanna declare a new pointer to function foo using variable p to make use of typedef statement like:

typedef int emp[10]; 
emp p;// so now p is an array of size 10 of type int 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    **What is** your question? –  Apr 12 '13 at 19:50
  • I wanna declare a new pointer to function foo using variable p to make use of typedef statement. like typedef int emp[10]; emp p;// so now p is an array of size 10 of type int –  Apr 12 '13 at 19:53
  • 1
    Then what you have (`typedef int (*p)();`) is fine. The name `p` will alias this type. –  Apr 12 '13 at 19:55
  • `p` is the identifer for a pointer type. Just make objects belonging to that type and assign to them: `p myptr1, q; myptr1 = foo; q = quux; myptr1() /* call foo() */; q() /* call quux() */;` – pmg Apr 12 '13 at 19:56
  • so tell me the syntax to declare a new pointer to funtion let say 'q' using 'p' –  Apr 12 '13 at 19:58
  • a link for you: http://www.newty.de/fpt/fpt.html#chapter2 – Grijesh Chauhan Apr 12 '13 at 20:25
  • @GrijeshChauhan thnx a lot.. really helpful –  Apr 12 '13 at 20:35

4 Answers4

1
int foo()
{
   // do something
}

typedef int (*p)();

int main( int argc, char** argv )
{
    int result;

    p funcPtr = foo;    
    result = funcPtr();

    return 0;
}
K Scott Piel
  • 4,320
  • 14
  • 19
  • I wanna declare a new pointer to function foo using variable p to make use of typedef statement. like typedef int emp[10]; emp p;// so now p is an array of size 10 of type int –  Apr 12 '13 at 19:52
  • 1
    -1: In your code `p` is a type (sorta). You cannot assign to it. – pmg Apr 12 '13 at 19:55
  • Now it's ok. I revoked the -1. – pmg Apr 12 '13 at 20:33
1
  1. p is:

    typedef int (*p) ();
    
  2. foo() is:

    int foo(){
    }
    
  3. p type variable is f:

    p  f = &foo; 
    
  4. how to call using pointer:

    (*f)();
    

Example code:

#include<stdio.h>
int foo(){
 printf("\n In FOO\n");
 return 4;
}
typedef int(*p)();

int main(){
 p f = &foo;
 int i = (*f)();
 printf("\n i = %d\n", i);
 return 1;
}

you can find it is working on codepad.

note: you can simply assign like p f = foo; and call like f() the second form you can find here on codepad

Edit: As @Akash commented:

it compiles and runs like:

~$ gcc x.c -Wall 
~$ ./a.out 
 In FOO
 i = 4

Here is a project to help explain the usefulness of function pointers.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • @pmg Yes :) , and also `p f = foo;` works well ...Just to emphasis on pointer I written in this way. – Grijesh Chauhan Apr 12 '13 at 20:02
  • @Girijesh I hv tried this already.. It gives following 2 errors 1Must take addrs of a memory allocation 2Declaration Syntax error –  Apr 12 '13 at 20:04
  • @Aakash you might be doing some error, check the link I proved its working. – Grijesh Chauhan Apr 12 '13 at 20:07
  • @gaurav just one doubt related to this case... Why doesn't removing "int" in typedef statemnt in my question generates any error –  Apr 12 '13 at 20:14
  • @Aakash name is Grijesh, Actually I forgot that by mistake earlier, not corrected, Although default return type of a function assumes to be int that what no error at compilation time, Second thing is I updated with one more technique as suggested by Mr. Pmg, find the 2nd codepad link in answer – Grijesh Chauhan Apr 12 '13 at 20:20
  • @Girijesh thnx a ton.. n sory for not getting ur name correct –  Apr 12 '13 at 20:28
  • @Aakash Welcome, and No problem .. check the commented link to your question and **wait** I am giving you a good book on this.. – Grijesh Chauhan Apr 12 '13 at 20:29
  • You don't need the `&` in `p f = &foo;` (you could write `p f = foo;`). – Jonathan Leffler Apr 12 '13 at 20:35
  • @Aakash get the book from answer. – Grijesh Chauhan Apr 12 '13 at 20:36
  • @JonathanLeffler Yes I have already added a note at bottom in my answer, Mr. Pmg also suggest me that but I wanted to emphasis on pointer/address. Also other answer have the way you are suggesting. My should be different thats the reason... Thanks :) – Grijesh Chauhan Apr 12 '13 at 20:39
0

With a typedef:

typdef int (*FunctionPtr)();

int foo()
{
    return 4; // chosen by fair dice roll, should be random
}

FunctionPtr fptr = foo; // or &foo

Without a typedef:

int (*fptr)() = foo; // or &foo
  • That is absolutely fine.... But I want to declare a new pointer to function other than ur (*FunctionPtr)() using (*FunctionPtr)() to make use of typedef statement –  Apr 12 '13 at 20:00
  • @Aakash I absolutely don't understand your question. What's not "new enough" in my approach? Try rephrasing your requirement using a better English, I have no idea what you are talking about. –  Apr 12 '13 at 20:01
  • I want to do following but it gives error typedef int (*p)(); int foo() {} int main() { int c; p q; q=fun() c=(*q)(); return 0; } –  Apr 12 '13 at 20:08
  • @Aakash 1. Write that in your question, 2. format it. Then I can be bothered reading it. –  Apr 12 '13 at 20:09
  • look at gaurav chauhan's answer. that's what i wanted –  Apr 12 '13 at 20:18
  • @Aakash That's no different from mine. –  Apr 12 '13 at 20:19
0

From this post: convert C function array to c++ functions pointer array?

 #include <stdio.h>

void f1() {printf("helo\n");}
void f2() {printf("world\n");}

typedef struct func
{
    void (*f)();
}func;

int main()
{
    func a[] = {{f1}, {f2}};
    a[0].f();
    return 0;
}
Community
  • 1
  • 1
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44