What a prototype!!

3 views
Skip to first unread message

Srinath A

unread,
Aug 23, 2014, 1:07:54 AM8/23/14
to RFSW Google Group
Hi All,
    I happened to chance upon one function prototype declaration that stumped me. Even when I tried the Satyajit right to left technique I still could not find out what it means. To top it when we thought that such declarations were only for 'those' interviews, this is one that is used by Unix!!

void (*signal(int signo, void (*func)(int)))(int);  

   After reading the description in the book, I realized that this means the following .

1. signal is a pointer to a function that takes two arguments 
signo: an integer
func: A pointer to a function that takes an int argument and returns nothing
2. The function pointer signal also returns a pointer to a function which takes an int as argument and returns nothing i.e. same prototype as the second argument to signal.

Essentially signal function installs the argument function pointer (func) as a handler to a signal (similar to an interrupt) with signal number signo. It also returns the previous handler function to the caller.

Regards,

Srinath A

Srinath A

unread,
Aug 23, 2014, 1:10:54 AM8/23/14
to Srinath A, RFSW Google Group
Hi,
    Sorry, a small but important correction below in the bullet 1!

Regards,

Srinath A


From: 'Srinath A' via rfsw <rf...@googlegroups.com>
To: RFSW Google Group <rf...@googlegroups.com>
Sent: Saturday, August 23, 2014 10:37 AM
Subject: What a prototype!!

--
You received this message because you are subscribed to the Google Groups "rfsw" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rfsw+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


satyajit dutta

unread,
Aug 23, 2014, 2:51:09 AM8/23/14
to Srinath A, RFSW Google Group
Review comment: 
Code is hard to debug. Please make the code readable 
--

Regards,
Satyajit

El arroyo de la sierra
Me complace más que el mar
333.png

Lava Kumar Goli

unread,
Aug 23, 2014, 3:38:11 AM8/23/14
to satyajit dutta, Srinath A, RFSW Google Group
Srinath,

For your first statement to be true, shouldnt there be a () for *signal. () have high precedence over *.
For me it looks the following: Signal is a function taking two arguments returning pointer to function that takes int as argument.

Regards,
Lava
333.png

Thyagaraj Murthy

unread,
Aug 23, 2014, 3:41:19 AM8/23/14
to satyajit dutta, Srinath A, RFSW Google Group
Hi,

It, in deed, is stumping and very hard to read. (2) is quite difficult to understand and very confusing.

There is a similar declaration:

int (*(*foo)())();

Here there is an extra * (leftmost) that indicates there is a pointer being returned.

The meaning of the above declaration is:
foo is a pointer to a function that takes nothing and returns a pointer to function that takes nothing and returns int.

In the signal declaration case, what confused me was the fact the signal was a function and not function pointer, as we jump to immediate conclusion. Another confusing thing here is that the function signal and its arguments appear embedded inside the function pointer that function signal returns!!!

I am wondering if we can simply this as:

typedef void (*func_t)(int);
 
func_t signal(int signo, func_t func);


Regards
 --Thyagaraj.



On Sat, Aug 23, 2014 at 12:21 PM, satyajit dutta <satyaji...@gmail.com> wrote:
333.png

Ajith Kutty

unread,
Aug 23, 2014, 4:38:28 AM8/23/14
to Thyagaraj Murthy, satyajit dutta, Srinath A, RFSW Google Group
Phew... after half an hour of deliberation... 

The underlying interview question is how to declare a function which takes int as argument and returns a function pointer? Is it so that, then we have to qualify the returning function pointer as well in the function declaration and that's where it gets tricky? 

It was excellent from tiger to make it readable. 

I guess Srinath was trying to correct the initial statement of "Signal is a pointer to a function" to "Signal is a function" by blurring the two words?? If so, that would concur with what Lava mentioned and my understanding now as well. Agree?

For the first statement from Srinath to be correct it should have been like,

void ( *( *signal(int signo, void (*func)(int)) ) ) (int)

or much simply, as per Tiger's direction

func_t (*signal) (int signo, func_t func);


Thyagaraj Murthy

unread,
Aug 23, 2014, 6:22:41 AM8/23/14
to Ajith Kutty, satyajit dutta, Srinath A, RFSW Google Group
Hi Ajith,

If the interviewer asks to write declaration given the description, then most of us would write the way I have written. So that would not be a tough interview question at all. Rather the question would be to understand the declaration and then explain what it is.

Also,

Ajith wrote:
-------------------
For the first statement from Srinath to be correct it should have been like,

void ( *( *signal(int signo, void (*func)(int)) ) ) (int)
-----------------------------

I am not sure if the above declaration is correct for Srinath's first statement. I think this should be:
void ( *(*signal)(int signo, void (*func)(int) ) ) (int)

(I have just rearranged parentheses around 'signal')

Regards
 --Thyagaraj.

333.png

Ajith Kutty

unread,
Aug 23, 2014, 6:33:59 AM8/23/14
to Thyagaraj Murthy, satyajit dutta, Srinath A, RFSW Google Group
Hi Tiger, 
            yes, I missed a parentheses and which is exactly why I would stick to the way you wrote it. So the really tough interview question would be to make such a function declaration in one statement!!!.
Cheers,
Ajith.

Thyagaraj Murthy

unread,
Aug 23, 2014, 8:48:47 AM8/23/14
to Ajith Kutty, satyajit dutta, Srinath A, RFSW Google Group
In this context, I remember reading another pointer question:

1) int (*ary)[10]; //ary is a pointer to array[10] of integer type -->sizeof returns 8 (assuming pointer size as 8 bytes)

2) int *ary[10]; //ary is an array[10] of pointers to integer type -->sizeof returns 80 (assuming pointer size as 8 bytes)

Parentheses in (1) changes the precedence. However, I don't know specific advantage of using pointer to a fixed array. Usage: (*ary)[i].

Regards
 --Thyagaraj.
333.png

Srinath A

unread,
Aug 24, 2014, 2:53:39 AM8/24/14
to Thyagaraj Murthy, Ajith Kutty, satyajit dutta, RFSW Google Group
Hi,
    For Lava's point, that was my correction i.e. signal is not a pointer to a function although I was probably not clear like Ajith mentioned.
    For Thyagaraj's point I think that the Unix guys have also used the alternative typedef format elsewhere.

    I also dont know of any real use of the point 1 below but maybe we can interpret the first dimension of a two dimensional array as a pointer to the array[10].

Regards,

Srinath A


From: Thyagaraj Murthy <murth...@gmail.com>
To: Ajith Kutty <ajith....@gmail.com>
Cc: satyajit dutta <satyaji...@gmail.com>; Srinath A <srinat...@yahoo.com>; RFSW Google Group <rf...@googlegroups.com>
Sent: Saturday, August 23, 2014 6:18 PM
Subject: Re: What a prototype!!
333.png

Thyagaraj Murthy

unread,
Aug 24, 2014, 3:23:35 AM8/24/14
to Srinath A, Ajith Kutty, satyajit dutta, RFSW Google Group
Hi Srinath,

>>    I also dont know of any real use of the point 1 below but maybe we can interpret the first dimension of a two dimensional array as a pointer to the array[10]. 
Yes. That is one use. But I initially thought there may other advantages in using pointer to array[n] rather than using plain pointers, but it appears there is none. It is simpler to use pointers as the syntax for using them are pretty simple and aligns with array usage syntax.

int array[10];
int (*ary)[10] = array;
int *ptr = array;

//usage
(*ary)[0] = 10; //complex syntax
ptr[0] = 10;     //simple syntax

Regards
 --Thyagaraj.


Reply all
Reply to author
Forward
0 new messages