473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 3657
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.