[comp.std.c] Is this a bug in the standard?

rfg@riunite.ACA.MCC.COM (Ron Guilmette) (05/01/89)

Well, I hope that Subject: line caught your attention.

My question is simple.  (Please excuse me if this has
already been hashed and re-hashed here before).

Is there any completely type-safe way to declare a function
(using function-prototype style) which can accept a
pointer to itself as a parameter?

-- 
// Ron Guilmette  -  MCC  -  Experimental Systems Kit Project
// 3500 West Balcones Center Drive,  Austin, TX  78759  -  (512)338-3740
// ARPA: rfg@mcc.com
// UUCP: {rutgers,uunet,gatech,ames,pyramid}!cs.utexas.edu!pp!rfg

friedl@vsi.COM (Stephen J. Friedl) (05/04/89)

In article <189@riunite.ACA.MCC.COM>, rfg@riunite.ACA.MCC.COM (Ron Guilmette) writes:
> Is there any completely type-safe way to declare a function
> (using function-prototype style) which can accept a
> pointer to itself as a parameter?

Do I smell a contest brewing?

    Steve :-)

-- 
Stephen J. Friedl / V-Systems, Inc. / Santa Ana, CA / +1 714 545 6442 
3B2-kind-of-guy   / friedl@vsi.com  / {attmail, uunet, etc}!vsi!friedl

Breaking a collerbone is a great way to cut down typing speed :-(

diamond@diamond.csl.sony.junet (Norman Diamond) (05/08/89)

In article <189@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes:

>Well, I hope that Subject: line caught your attention.

Well, I think it's a bug in the standard.  And in C++, where you first
asked this question.  At least this one is a reasonable bug, i.e. hard
to foresee.

>Is there any completely type-safe way to declare a function
>(using function-prototype style) which can accept a
>pointer to itself as a parameter?

Seems to me that ARK's answer in comp.lang.c++ is correct; it's impossible.

But if you're content to wrap it in a struct, you get type-safety:


union bar_t;

typedef int foo_t (union bar_t);   /* gets a warning from gcc 1.32 */

typedef foo_t *foo_ptr_t;

typedef union bar_t {
    foo_ptr_t foo_ptr;
} bar_t;

foo_t foo;                         /* your prototype is done, sir */

bar_t make_bar (foo_ptr_t a_foo_ptr)
{
    bar_t my_bar;
    my_bar.foo_ptr = a_foo_ptr;
    return my_bar;
}

int foo (bar_t bar)
{
    if (bar.foo_ptr == &foo)
    {
	printf ("Hello world, it's me!\n");
	return 0;
    } else {
	printf ("Goodbye, cruel world!\n");
	return 1;
    }
}

main()
{
   foo (make_bar (&foo));
}

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?

diamond@diamond.csl.sony.junet (Norman Diamond) (05/08/89)

I just wrote:

>But if you're content to wrap it in a struct, you get type-safety:
>
>union bar_t;
>
>typedef int foo_t (union bar_t);   /* gets a warning from gcc 1.32 */

Correction, gets an _error_ from gcc 1.32.  You have to pass a pointer
to the struct.

typedef union bar_t *bar_ptr_t;

typedef int foo_t (bar_ptr_t);      /* gets a warning from gcc 1.32 */

>typedef foo_t *foo_ptr_t;
>
>typedef union bar_t {
>    foo_ptr_t foo_ptr;
>} bar_t;
>
>foo_t foo;                         /* your prototype is done, sir */
>
>bar_t make_bar (foo_ptr_t a_foo_ptr)

Uh, make_bar_ptr ....

>{
>    bar_t my_bar;

Uh, ummm, hmmmmm......

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?