List Info

Thread: Anonymous structures




Anonymous structures
user name
2006-08-04 15:28:37
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
-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
Anonymous structures
user name
2006-08-04 17:22:35
krishna.vamsiwipro.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 <glynngclements.plus.com>
-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
Anonymous structures
user name
2006-08-05 02:28:12
Hi Glynn,

On 8/4/06, Glynn Clements <glynngclements.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.livejourn
al.com
-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
Anonymous structures
user name
2006-08-05 16:20:37
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 <glynngclements.plus.com>
-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )