Simple question about Pointers to Functions 
Author Message
 Simple question about Pointers to Functions

Hi Netters

I have a really simple question.

       1: #include <stdio.h>
       2:
       3: main()
       4: {
       5:         void (*ptr)();
       6:         void test();
       7:
 >     8:         ptr = test;
       9:         ptr();
      10:         (*ptr)();
      11:
      12: }
      13:
      14: void test()
      15: {
      16:         printf(" this is a test\n");
      17: }
      18:

both calls on line 9 and 10 work, but I am not quite sure I understand
why the call ptr() works. (correct way to call is on line 10). Can
someone be kind as to shed light on the subject ????

Thanks

Madhavi



Tue, 02 Jan 1996 21:12:52 GMT  
 Simple question about Pointers to Functions

Quote:

>       5:         void (*ptr)();
>       6:         void test();
>       7:
> >     8:         ptr = test;
>       9:         ptr();
>      10:         (*ptr)();
>both calls on line 9 and 10 work, but I am not quite sure I understand
>why the call ptr() works. (correct way to call is on line 10). Can
>someone be kind as to shed light on the subject ????

ANSI C has allowed you to call the function through the pointer using
normal function calling convention.  Without that allowance, line 9
would probably fail.  (ie, it doesn't really make sense, but it's
convenient, so they _defined_ lines 9 and 10 to mean the same thing.)

--
If the Earth is the size of a pea in New York, then the Sun is a beachball 50m
away, Pluto is 2km away, and the next nearest star is in Tokyo.  Now shrink
Pluto's orbit into a coffee cup; then our Milky Way Galaxy fills North America.



Tue, 02 Jan 1996 22:17:58 GMT  
 Simple question about Pointers to Functions

Quote:

> >       5:         void (*ptr)();
> >       6:         void test();
> >       7:
> > >     8:         ptr = test;
> >       9:         ptr();
> >      10:         (*ptr)();

> >both calls on line 9 and 10 work, but I am not quite sure I understand
> >why the call ptr() works. (correct way to call is on line 10). Can
> >someone be kind as to shed light on the subject ????

> ANSI C has allowed you to call the function through the pointer using
> normal function calling convention.  Without that allowance, line 9
> would probably fail.  (ie, it doesn't really make sense, but it's
> convenient, so they _defined_ lines 9 and 10 to mean the same thing.)

Um I must disagree, we dont have any problems with arrays and pointers to arrays
being handled differently, therefore line 9 is preferable. The () have the
same significance for functions as [] for arrays. The point is that there can be only
1 meaning so addign a * or an & is irelevant. My preffered route is:-

typedef void Func_t(void);

Func_t test;     /* forward decleration of test */

  Func_t *Func;  /* decleration of pointer to a function of type:- void name(void) */
  Func = test;

  Func();        /* calls the function test */

You can say Func = &test; for the assignement and you can say (*Func)() to call the
function. But then the syntax looks mighty awfull.

--
=============================================================================

                                        Fax: +44 (0)31-667 7209
=============== So long and thanks for all the fish =========================



Fri, 05 Jan 1996 18:21:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question about signal()/pointers to functions that return pointers to functions

2. How to use (pointer to function), and function and pointer to (pointer to function)

3. Pointer Functions and Pointers to Pointer Functions

4. Simple character pointer/function problem.

5. Simple character pointer/function problem

6. Callback function/function pointer question

7. Simple, Pass Pointer of an Array to function

8. QUESTION: functions returning pointers to functions

9. a question on function returning a pointer to a function

10. Very simple questions about pointers

11. pointer to a structure (simple question for a real C programmer)

12. Another very simple pointer question

 

 
Powered by phpBB® Forum Software