473,769 Members | 5,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing compound type containing opaque type

Consider the following pseudo-code:

#include <opaque.h>

struct foo {
int a;
opaque_t op;
int b;
};

struct foo blah = { 17, /* ??? */ , 23 };

Here we suppose that `opaque_t' is defined in <opaque.has some type
not known to us, or which we cannot rely on. In particular, it may be
a scalar or compound type. The goal is to initialize `blah' in such a
way that `blah.a == 17' and `blah.b == 23'. We don't care about the
value of `blah.op'.

It seems to me that there is no way to do this in standard C. Is this
correct, or am I missing something?

A workaround would be to rearrange the members of `struct foo' as

struct foo2 {
int a;
int b;
opaque_t op;
};

struct foo2 blah2 = { 17, 23 };

I believe this is legal, but I could be wrong. My compiler warns
about a missing initializer, but accepts the code.

If `opaque_t' is known to be a compound type, we could write

struct foo blah3 = { 17, { }, 23 };

which again provokes a missing initializer warning but is accepted.
Again I am curious whether it is actually legal.

I thought of this issue after reading a post here a couple weeks ago,
"Exception handling crashes or exits" by Fabiano Sidler,
<48**********@n ews.bluewin.ch> , in which the poster wants a struct
with a jmp_buf as one member. He initializes that member with { 0 },
which is certainly non-portable. But I wondered if there is a
portable way to do the same thing.

If not, it seems like it might be useful for the authors of <opaque.h>
to provide a macro which is suitable as an initializer for opaque_t.
E.g. <opaque.hmigh t contain

typedef opaque_t struct {
int x,
double d;
};

#define OPAQUE_INITIALI ZER { 0, 0.0 }

so that one could write

struct foo blah = { 17, OPAQUE_INITIALI ZER, 23 };

In particular, this could be a useful extension for library authors to
provide for opaque library types (e.g. jmp_buf, FILE, etc).

Any thoughts?
Oct 1 '08
27 2528
On 1 Oct, 22:05, Nate Eldredge <n...@vulcan.la nwrote:
Consider the following pseudo-code:

#include <opaque.h>

struct foo {
* * * *int a;
* * * *opaque_t op;
* * * *int b;

};
If opaque_t is truly opaque, and not specified
in opaque.h, then the compiler won't accept
this definition of struct foo. If the compiler
is accepting this, then the header is specifying
opaque_t completely, so (I would argue) has
misnamed it because it isn't opaque.
struct foo blah = { 17, /* ??? */ , 23 };

Here we suppose that `opaque_t' is defined in <opaque.has some type
not known to us, or which we cannot rely on.
I don't understand what you mean by "not known to us". If
the compiler can get the definition from opaque_t, then
you can, too! Am I using the word "opaque" in a
stricter sense than you? When I say opaque type,
I mean that the header contains no more than:
typedef struct opaque * opaque_t;
or just:
struct opaque;

<snip>
If not, it seems like it might be useful for the authors of <opaque.h>
to provide a macro which is suitable as an initializer for opaque_t.
E.g. <opaque.hmigh t contain

typedef opaque_t struct {
* * * * int x,
* * * * double d;

};
Is this an opaque type? It's got an int and
a double, so ... no, it's not. At least not
by my understanding of the word opaque.

Oct 2 '08 #11
On 2 Oct, 00:45, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
vipps...@gmail. com writes:

<snip>
You can also do this for array elements
char foo[2] = { .[1] = 'a' };

There's no dot. *I.e.:

* char foo[2] = { [1] = 'a' };
Is that C99? I thought that was a gnu extension.

Oct 2 '08 #12
William Pursell <bi**********@g mail.comwrites:
On 1 Oct, 22:05, Nate Eldredge <n...@vulcan.la nwrote:
>Consider the following pseudo-code:

#include <opaque.h>

struct foo {
* * * *int a;
* * * *opaque_t op;
* * * *int b;

};

If opaque_t is truly opaque, and not specified
in opaque.h, then the compiler won't accept
this definition of struct foo. If the compiler
is accepting this, then the header is specifying
opaque_t completely, so (I would argue) has
misnamed it because it isn't opaque.
By "opaque" I mean an object whose format and contents are not
intended to be used by the programmer, and are subject to change.
This is how I've heard it used in the past.

jmp_buf is an example. Certainly you can open up <setjmp.hand find
out how it is defined on your system; maybe it's a struct with a
member for each of your CPU's registers. But you can't use any of
that information in a portable program, since on another system, or a
later version of the same system, it may be defined differently, or
used differently by the library, so in a more general sense you don't
really "know" how it's defined. All you "know" about jmp_buf is that
you can create one using setjmp() and pass it to longjmp() later on.
From the programmer's point of view it's just a magic cookie.

Other examples in the standard library would be time_t and FILE.
div_t is a library type that I would not call opaque; it is documented
as being a struct with two members, `quot' and `rem', whose types and
meanings are well described and guaranteed to be the same in all
conforming implementations .

Oct 2 '08 #13
William Pursell <bi**********@g mail.comwrites:
On 2 Oct, 00:45, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>vipps...@gmail .com writes:

<snip>
You can also do this for array elements
char foo[2] = { .[1] = 'a' };

There's no dot. Â*I.e.:

Â* char foo[2] = { [1] = 'a' };

Is that C99? I thought that was a gnu extension.
Yes, it is C99:

designation:
designator-list =

designator-list:
designator
designator-list designator

designator:
[ constant-expression ]
. identifier

--
Ben.
Oct 2 '08 #14
William Pursell wrote:
On 2 Oct, 00:45, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
....
> char foo[2] = { [1] = 'a' };

Is that C99? ...
Yes.

Oct 2 '08 #15
William Pursell <bi**********@g mail.comwrites:
On 1 Oct, 22:05, Nate Eldredge <n...@vulcan.la nwrote:
>Consider the following pseudo-code:

#include <opaque.h>

struct foo {
* * * *int a;
* * * *opaque_t op;
* * * *int b;

};

If opaque_t is truly opaque, and not specified
in opaque.h, then the compiler won't accept
this definition of struct foo. If the compiler
is accepting this, then the header is specifying
opaque_t completely, so (I would argue) has
misnamed it because it isn't opaque.
There are different kinds of opacity. Consider type FILE, defined in
<stdio.h>. That header must completely specify the type -- but code
that uses it is expected to depend only on the standard functions and
the fact that it's an object type.
>struct foo blah = { 17, /* ??? */ , 23 };

Here we suppose that `opaque_t' is defined in <opaque.has some type
not known to us, or which we cannot rely on.

I don't understand what you mean by "not known to us". If
the compiler can get the definition from opaque_t, then
you can, too!
Sure you *can* -- but then the next version of the library defines it
differently and your code breaks.

It's both common and reasonable to define an "opaque" type that's
completely defined in the header, just by *asking* authors of client
code not to cheat by peeking at its innards. (Which is not to say
that there aren't other reasonable approaches.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 2 '08 #16
Nick Keighley <ni************ ******@hotmail. comwrites:
On 2 Oct, 17:28, Keith Thompson <ks...@mib.orgw rote:

<snip>
>There are different kinds of opacity. *Consider type FILE, defined in
<stdio.h>. *That header must completely specify the type

why? user code only needs a pointer
>-- but code
that uses it is expected to depend only on the standard functions and
the fact that it's an object type.

<snip>
The standard requires FILE to be

an object type capable of recording all the information needed to
control a stream, including its file position indicator, a pointer
to its associated buffer (if any), an _error indicator_ that
records whether a read/write error has occurred, and an
_end-of-file indicator_ that records whether the end of the file
has been reached

The declaration of FILE in <stdio.hneedn 't indicate *how* it records
all that information. For example, it could be declared as

typedef unsigned char[_FILE_BYTES] FILE;

and the library code that uses it could cast the FILE* to, say,
_FILE_STRUCT* to access the information. Most implementations don't
do this; for one thing, the macro definitions for putc and getc are
going to expose some of the inner details anyway.

Since user code only needs to use FILE*, not type FILE itself, an
implementation that declares:

typedef void FILE;

would be perfectly sensible, except that it would violate the
standard's requirement. In my opinion, the standard over-specifies
this, but it's not a huge deal.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 3 '08 #17


Nate Eldredge wrote:
Eric Sosman <Er*********@su n.comwrites:
In C90 you could initialize the unknown member to "zero of
the appropriate type:"

struct foo blah = { 17, { 0 }, 23 };

Some compilers may emit warnings for too few initializers, but
the initialization is valid; if opaque_t is compound, its sub-
elements are initialized to appropriate zeroes. The warnings may
be a nuisance, but they do no actual harm.

Interesting, so the OP was correct after all. I never knew about
"zero of the appropriate type". I assumed that would be wrong if
opaque_t turned out to be a scalar type, or a compound whose first
member was also compound, but apparently not. Those clever language
designers, always one step ahead. :)
{0} initializer is wrong if the first member of opaque_t is an array
Oct 3 '08 #18
Szabolcs Nagy <ns*******@gmai l.comwrites:
Nate Eldredge wrote:
>Eric Sosman <Er*********@su n.comwrites:
In C90 you could initialize the unknown member to "zero of
the appropriate type:"

struct foo blah = { 17, { 0 }, 23 };

Some compilers may emit warnings for too few initializers, but
the initialization is valid; if opaque_t is compound, its sub-
elements are initialized to appropriate zeroes. The warnings may
be a nuisance, but they do no actual harm.

Interesting, so the OP was correct after all. I never knew about
"zero of the appropriate type". I assumed that would be wrong if
opaque_t turned out to be a scalar type, or a compound whose first
member was also compound, but apparently not. Those clever language
designers, always one step ahead. :)

{0} initializer is wrong if the first member of opaque_t is an array
Is it really? gcc -ansi -pedantic accepts it.
Oct 3 '08 #19
On Oct 4, 12:41 am, Nate Eldredge <n...@vulcan.la nwrote:
Szabolcs Nagy <nszabo...@gmai l.comwrites:
Nate Eldredge wrote:
Eric Sosman <Eric.Sos...@su n.comwrites:
In C90 you could initialize the unknown member to "zero of
the appropriate type:"
struct foo blah = { 17, { 0 }, 23 };
Some compilers may emit warnings for too few initializers, but
the initialization is valid; if opaque_t is compound, its sub-
elements are initialized to appropriate zeroes. The warnings may
be a nuisance, but they do no actual harm.
Interesting, so the OP was correct after all. I never knew about
"zero of the appropriate type". I assumed that would be wrong if
opaque_t turned out to be a scalar type, or a compound whose first
member was also compound, but apparently not. Those clever language
designers, always one step ahead. :)
{0} initializer is wrong if the first member of opaque_t is an array

Is it really? gcc -ansi -pedantic accepts it.

No it isn't, it's correct.

When not all of an object is initialized, the rest is initialized as
if it were 'static'.
Oct 3 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
4558
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
1
5066
by: Labora | last post by:
Hi All, Could you tell how to implement opaque data type with an example ? Or link to a documentation will be great !! Thanks, Labora.
26
3132
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The code will not be idiomatic C. GCC has an extension to ISO C that permits nested functions: <http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html> For implementing closures they have a serious limitation:
6
2674
by: William Ahern | last post by:
So, GCC 4.01 is giving errors that GCC 3.3 did not, and I'm thinking they've gone overboard with their new type checking infrastructure. Here's the supposedly offending code (no laughing or grimacing, please ;) char *s, *s1; s1 = strcpy((char ){ },s); and GCC 4.01's error messages
7
1937
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage duration associated with the enclosing block". So I tried the annexed code, and it compiles without a warning, and works as I expected.
3
6643
by: Richard Weeks | last post by:
I have a library of functions for operations on complex numbers and a complex type defined as a struct in a header also containing the prototypes for the library. I want to make this an opaque type, i.e. I don't want the real and imaginary fields to be visible to the user of the interface. I tried doing this: static struct cplex { double real; double imag; };
12
2294
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
0
9589
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
9423
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
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
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
6674
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
5304
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...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.