By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

Portable struct initialization

P: n/a
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:

/* NetBSD - /usr/include/netinet6/in6.h */
struct in6_addr {
union {
__uint8_t __u6_addr8[16];
__uint16_t __u6_addr16[8];
uint32_t __u6_addr32[4];
} __u6_addr; /* 128-bit IP6 address */
};

/* Linux - /usr/include/netinet/in.h */
struct in6_addr
{
union
{
uint8_t u6_addr8[16];
uint16_t u6_addr16[8];
uint32_t u6_addr32[4];
} in6_u;
};

/* Solaris - /usr/include/netinet/in.h */
struct in6_addr {
union {
uint8_t _S6_u8[16]; /* IPv6 address */
uint32_t _S6_u32[4]; /* IPv6 address */
uint32_t __S6_align; /* Align on 32 bit
boundary */
} _S6_un;
};

Is there a way to build a portable static initializer
for struct in6_addr, other than selecting among several
compile-time codes by using precompiler directives?
Or else, would it be safer to use a non-static initialization?
Please advise.

Dec 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Everton wrote:
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:


If the variable is non-local then

struct in6_addr myVariable;

If the variable is local, in which case you have to use a
dynamic initialisation, then:

static const struct in6_addr zero;

... inside a function ...
struct in6_addr myLocal = zero;

--
Chris "looks initialised to /me/" Dollin
oxygen is a highly reactive waste-product of plant life.
Dec 20 '05 #2

P: n/a
Everton wrote:
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:

/* NetBSD - /usr/include/netinet6/in6.h */
struct in6_addr {
union {
__uint8_t __u6_addr8[16];
__uint16_t __u6_addr16[8];
uint32_t __u6_addr32[4];
} __u6_addr; /* 128-bit IP6 address */
};

/* Linux - /usr/include/netinet/in.h */
struct in6_addr
{
union
{
uint8_t u6_addr8[16];
uint16_t u6_addr16[8];
uint32_t u6_addr32[4];
} in6_u;
};

/* Solaris - /usr/include/netinet/in.h */
struct in6_addr {
union {
uint8_t _S6_u8[16]; /* IPv6 address */
uint32_t _S6_u32[4]; /* IPv6 address */
uint32_t __S6_align; /* Align on 32 bit
boundary */
} _S6_un;
};

Is there a way to build a portable static initializer
for struct in6_addr, other than selecting among several
compile-time codes by using precompiler directives?
Or else, would it be safer to use a non-static initialization?
Please advise.


If you want them initialised to 0 then it is easy.
struct in6_addr fred = { 0 };
Alternatively, since this is obviously an instance where all bits 0 is
going to be valid (knowledge based on stuff outside the C standard), you
can use:
memset(fred, 0, sizeof fred);

However, this does not help you with accessing the individual elements.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 20 '05 #3

P: n/a
I'm trying to figure out whether it would be safe (portable)
to define a static initialization like the example below.
Could alternate definitions of in6_addr's members
lead to unexpected behavior on some platform,
yielding an address distinct from the one expected?

$ more init.c
/* init.c */
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>

struct in6_addr addr = { 1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16 };

int main() {
unsigned char *i = (unsigned char *) &addr;
unsigned char *past_end = i + 16;
for (; i < past_end; ++i) printf("%d ", *i);
printf("\n");
exit(0);
}
$ gcc -Wall -o init init.c
init.c:7: warning: missing braces around initializer
init.c:7: warning: (near initialization for `addr.in6_u')
$ ./init
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$

Dec 20 '05 #4

P: n/a
Everton wrote:
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:

Isn't it more interresting HOW you want to initialize them ?
Quite so often you want to place a converted textual ip address in
there, and there are posix functions for doing so. Use those.

Dec 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.