* Anonymous structures
@ 2006-08-04 15:28 krishna.vamsi
2006-08-04 17:22 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: krishna.vamsi @ 2006-08-04 15:28 UTC (permalink / raw)
To: linux-c-programming
Hi all,
Can I pass anonymous structures as function arguments,
Struct A{ int a; int b};
F(struct A a)
{
}
Int main()
{
f({a,b})
}
If I can how can I do that ?
-Vamsi
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Anonymous structures
2006-08-04 15:28 Anonymous structures krishna.vamsi
@ 2006-08-04 17:22 ` Glynn Clements
2006-08-05 2:28 ` Raseel Bhagat
0 siblings, 1 reply; 4+ messages in thread
From: Glynn Clements @ 2006-08-04 17:22 UTC (permalink / raw)
To: krishna.vamsi; +Cc: linux-c-programming
krishna.vamsi@wipro.com wrote:
> Can I pass anonymous structures as function arguments,
>
> Struct A{ int a; int b};
>
> F(struct A a)
> {
> }
>
> Int main()
> {
> f({a,b})
> }
>
> If I can how can I do that ?
No. An anonymous structure has an unknown size, so the compiler cannot
generate code to copy them.
Anonymous structures are only useful as pointer targets, i.e.
"struct A *foo". You can pass around pointers to structures without
knowing the size of the structure, although you can't dereference them.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Anonymous structures
2006-08-04 17:22 ` Glynn Clements
@ 2006-08-05 2:28 ` Raseel Bhagat
2006-08-05 16:20 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: Raseel Bhagat @ 2006-08-05 2:28 UTC (permalink / raw)
To: linux-c-programming
Hi Glynn,
On 8/4/06, Glynn Clements <glynn@gclements.plus.com> wrote:
> No. An anonymous structure has an unknown size, so the compiler cannot
> generate code to copy them.
>
> Anonymous structures are only useful as pointer targets, i.e.
> "struct A *foo". You can pass around pointers to structures without
> knowing the size of the structure, although you can't dereference them.
>
>
If we can't dereference them, then what would be the use of such a call ?
Can you cite a example for the above scenario ?
--
Raseel.
http://osd.byethost8.com
http://raseel.livejournal.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Anonymous structures
2006-08-05 2:28 ` Raseel Bhagat
@ 2006-08-05 16:20 ` Glynn Clements
0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2006-08-05 16:20 UTC (permalink / raw)
To: Raseel Bhagat; +Cc: linux-c-programming
Raseel Bhagat wrote:
> > No. An anonymous structure has an unknown size, so the compiler cannot
> > generate code to copy them.
> >
> > Anonymous structures are only useful as pointer targets, i.e.
> > "struct A *foo". You can pass around pointers to structures without
> > knowing the size of the structure, although you can't dereference them.
>
> If we can't dereference them, then what would be the use of such a call ?
> Can you cite a example for the above scenario ?
They can be dereferenced in a context where the structure isn't
anonymous.
A library can define a structure in a private header which is used by
the library's source files, while the public header only has an
anonymous declaration. The library can pass pointers to the
application, which can pass them back to the library, but the
application can't access the structure's members. To the application,
the pointer is an opaque handle.
E.g.:
private header:
struct foo { int x; };
library source:
#include "foo_priv.h"
struct foo *foo_new(void)
{
struct foo *p = malloc(sizeof(struct foo));
p->x = 0;
return p;
}
void foo_set(struct foo *p, int x)
{
p->x = x;
}
int foo_get(struct foo *p)
{
return p->x;
}
public header:
struct foo;
extern struct foo *foo_new(void);
extern void foo_set(struct foo *p, int x);
extern int foo_get(struct foo *p);
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-08-05 16:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-04 15:28 Anonymous structures krishna.vamsi
2006-08-04 17:22 ` Glynn Clements
2006-08-05 2:28 ` Raseel Bhagat
2006-08-05 16:20 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).