LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2007, 03:11 AM   #1
KasperLotus
LQ Newbie
 
Registered: Aug 2005
Distribution: Fedora Core 6
Posts: 21

Rep: Reputation: 15
typedef a union as a struct?


Well, I don't give myself much credit when it comes to C but I'm having a really hard time with this particular problem. Here's the bit of code I'm struggling with, it's from /usr/include/sys/socket.h

Code:
# define __SOCKADDR_ARG         struct sockaddr *__restrict
# define __CONST_SOCKADDR_ARG   __const struct sockaddr *
#else
/* Add more `struct sockaddr_AF' types here as necessary.
   These are all the ones I found on NetBSD and Linux.  */
# define __SOCKADDR_ALLTYPES \
  __SOCKADDR_ONETYPE (sockaddr) \
  __SOCKADDR_ONETYPE (sockaddr_at) \
  __SOCKADDR_ONETYPE (sockaddr_ax25) \
  __SOCKADDR_ONETYPE (sockaddr_dl) \
  __SOCKADDR_ONETYPE (sockaddr_eon) \
  __SOCKADDR_ONETYPE (sockaddr_in) \
  __SOCKADDR_ONETYPE (sockaddr_in6) \
  __SOCKADDR_ONETYPE (sockaddr_inarp) \
  __SOCKADDR_ONETYPE (sockaddr_ipx) \
  __SOCKADDR_ONETYPE (sockaddr_iso) \
  __SOCKADDR_ONETYPE (sockaddr_ns) \
  __SOCKADDR_ONETYPE (sockaddr_un) \
  __SOCKADDR_ONETYPE (sockaddr_x25)

# define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
typedef union { __SOCKADDR_ALLTYPES
              } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
If I mentally expand all of that I get something like "typedef union { all the sockaddr variations } struct sockaddr *__restrict __attribute__ ((__transparent_union__));" and there are a few things about that that I just don't understand.

I think I understand the transparent union attribute. It seems like it makes it somewhat of an "abstract class" in that any variable of the same type as any member of that union will be treated as that "union's type" without casting. Conceptually that makes sense, although it seems like passing a sockaddr_in as a sockaddr would cause problems unless the user programs are the only ones manipulating the actual structs and the kernel just passes them around, which is what I expect.

Beyond that, I have no idea what __restrict does, so I would appreciate it if someone could explain that to me.

And maybe it's just too late at night for me, but I don't see how (or why) you would typedef a union as a struct. The only thing I can think of is that maybe since the transparent_union attribute makes the union more like a class (of structs) it needs to be "named" a struct.

To help me figure this out, I tried to write a similar program (but much smaller ^_^) so I'll attach that in case I just misinterpreted something that was happening and incorporated it into my program incorrectly.

Code:
#include <stdio.h>

struct punk {
	char *name;
	int len;
};

struct bik {
	//long overmind;
	//int uid;
	char *death;
};

typedef union {
	struct punk *__restrict __punk__;
	struct bik *__restrict __bik__;
} struct punk *__restrict __attribute__ ((__transparent_union__));

int foo(struct punk *boo);

int main()
{
	struct punk lsa = {
		.name = "mugget",
		.len = 7,
	};
	struct bik asd = {
	//	.overmind = 8,
	//	.uid = 4,
		.death = "is coming",
	};
	foo(&asd);
}

int foo(struct punk *boo)
{
	switch(sizeof(boo)) {
		case sizeof(struct punk):
			printf("I'm a punk");
			break;
		case sizeof(struct bik):
			printf("I'm a bik");
			break;
	}
	return 0;
}
So I would appreciate it if anyone could clear any of those things up for me. And before you ask there's not really a solid goal behind all of this, I just wanted to know more about linux networking (boredom mostly).

Thanks in advance.

P.S. I know my variable names suck.
 
Old 03-09-2007, 05:32 AM   #2
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Code:
switch(sizeof(boo))
The sizeof() operator is evaluated at compile-time, not runtime, so this switch won't work. Also, boo is a pointer, not a struct or union, and all pointers have the same size.

I don't know if that's the issue you're looking for or not.
 
Old 03-09-2007, 09:27 AM   #3
KasperLotus
LQ Newbie
 
Registered: Aug 2005
Distribution: Fedora Core 6
Posts: 21

Original Poster
Rep: Reputation: 15
No, that wasn't the issue. I know it doesn't work and why, but 1. I didn't write that function originally to use pointers and 2. I don't care what happens in that function anymore. I was originally hoping to be able to distinguish between the types in the union but then my union wasn't working so I just left that alone.

Thanks for the reply though.
 
Old 03-09-2007, 01:10 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
gcc -E filename.h will show you what the final result of the code is.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing a struct within a struct? smoothdogg00 Programming 3 12-21-2006 01:38 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration