Re: Is this portable?

Lists: pgsql-hackers
From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Is this portable?
Date: 2007-04-02 19:22:08
Message-ID: 20070402192208.GH13946@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Can I declare a struct in a function's declaration section? Something
like this:

static void
foobar(void)
{
struct foo {
Oid foo;
int bar;
};

struct foo baz;

baz.foo = InvalidOid;
baz.bar = 42;

}

I tried here and GCC does not complain, with -std=c89 -pedantic.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is this portable?
Date: 2007-04-02 19:29:04
Message-ID: 200704021929.l32JT4Q00786@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera wrote:
> Can I declare a struct in a function's declaration section? Something
> like this:
>
> static void
> foobar(void)
> {
> struct foo {
> Oid foo;
> int bar;
> };
>
> struct foo baz;
>
> baz.foo = InvalidOid;
> baz.bar = 42;
>
> }
>
> I tried here and GCC does not complain, with -std=c89 -pedantic.

Sure.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Zdenek Kotala <Zdenek(dot)Kotala(at)Sun(dot)COM>
To: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is this portable?
Date: 2007-04-02 19:48:29
Message-ID: 46115E0D.7020208@sun.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera wrote:
> Can I declare a struct in a function's declaration section? Something
> like this:
>
> static void
> foobar(void)
> {
> struct foo {
> Oid foo;
> int bar;
> };
>
> struct foo baz;
>
> baz.foo = InvalidOid;
> baz.bar = 42;
>
> }
>
> I tried here and GCC does not complain, with -std=c89 -pedantic.
>

It works fine with Sun Studio 11.

Zdenek


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Zdenek Kotala <Zdenek(dot)Kotala(at)Sun(dot)COM>
Cc: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is this portable?
Date: 2007-04-02 20:33:46
Message-ID: 27676.1175546026@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Zdenek Kotala <Zdenek(dot)Kotala(at)Sun(dot)COM> writes:
> Alvaro Herrera wrote:
>> Can I declare a struct in a function's declaration section?

> It works fine with Sun Studio 11.

AFAICT it's required by the original K&R C book.

regards, tom lane


From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Zdenek Kotala" <Zdenek(dot)Kotala(at)Sun(dot)COM>, "Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is this portable?
Date: 2007-04-02 20:43:00
Message-ID: 877isumrsb.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:

> Zdenek Kotala <Zdenek(dot)Kotala(at)Sun(dot)COM> writes:
>> Alvaro Herrera wrote:
>>> Can I declare a struct in a function's declaration section?
>
>> It works fine with Sun Studio 11.
>
> AFAICT it's required by the original K&R C book.

IIRC there's something odd about the scope of the declared struct label.

Something like it previously extended to the end of the file but post-ANSI was
limited to the scope it's declared in (including very limited scopes where it
would be useless such as in function parameters).

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Zdenek Kotala <Zdenek(dot)Kotala(at)Sun(dot)COM>, Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is this portable?
Date: 2007-04-02 21:19:58
Message-ID: 20070402211958.GL13946@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Gregory Stark wrote:
> "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
>
> > Zdenek Kotala <Zdenek(dot)Kotala(at)Sun(dot)COM> writes:
> >> Alvaro Herrera wrote:
> >>> Can I declare a struct in a function's declaration section?
> >
> >> It works fine with Sun Studio 11.
> >
> > AFAICT it's required by the original K&R C book.
>
> IIRC there's something odd about the scope of the declared struct label.
>
> Something like it previously extended to the end of the file but post-ANSI was
> limited to the scope it's declared in (including very limited scopes where it
> would be useless such as in function parameters).

Hmm, thanks everybody. I was just going to say "bummer!" because I
needed to build a qsort comparator for these, but then I realized that
it's better if I keep worker and launcher database structs separate --
the only field they use in common is the Oid anyway.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: "Zdenek Kotala" <Zdenek(dot)Kotala(at)Sun(dot)COM>, "Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is this portable?
Date: 2007-04-02 21:30:58
Message-ID: 28899.1175549458@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Gregory Stark <stark(at)enterprisedb(dot)com> writes:
> IIRC there's something odd about the scope of the declared struct label.

> Something like it previously extended to the end of the file but post-ANSI was
> limited to the scope it's declared in (including very limited scopes where it
> would be useless such as in function parameters).

I think you might be thinking of the use of a previously unreferenced
"struct foo" in a function declaration's parameter list, which is
something that did change (and so gcc warns about it). But within a
block is not that case.

regards, tom lane