472,371 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

Portable struct initialization

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
4 3552
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Martin Vorbrodt | last post by:
Hi there. Is this code portable between platforms? Is it also 100% standard compliant? #include <iostream> using namespace std; class Point { public: enum COORDS { X = 0, Y, Z }; Point(int...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
5
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
10
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization....
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
32
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
10
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
5
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.