473,809 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3697
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
1730
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 x, int y, int z) : _x(x), _y(y), _z(z) {}
3
7663
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
5
2397
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 example below (from GBD's THE C BOOK) uses this. Is this a portable method? . /*
10
8650
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. That struck me as a bit odd really as I would have thought it would have only bothered about 'tracking' an 'atomic'/'complete' (via 'new()') initialization not doing it 'bit by it'!!! Any comments? Seems a bit of an overhead tracking memberwise...
4
5071
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
32
2072
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
18
9129
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: struct st_a { int i, j; };
10
5073
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
11678
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
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.