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? 27 2522
On Oct 2, 12:05 am, Nate Eldredge <n...@vulcan.la nwrote:
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.
Yes, it's legal.
(note: In POSIX it wouldn't be; *_t are reserved identifiers)
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.
Also legal.
I thought of this issue after reading a post here a couple weeks ago,
"Exception handling crashes or exits" by Fabiano Sidler,
<48c4e24d$...@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.
Yes, jmp_buf foo = {0};
Though that'd initialize all members to 0, 0.0 or NULL depending on
type, recursively applied for any aggregate
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.
Well, it is legal, so it's not useful.
E.g. <opaque.hmigh t contain
<snip>
Nate Eldredge wrote:
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'.
In C99 you could use a "compound literal:"
struct foo blah = (struct foo){.a = 17, .b = 23};
(Double-check my syntax; I'm typing this hurriedly.)
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.
-- Er*********@sun .com
On Wed, 01 Oct 2008 14:05:53 -0700, Nate Eldredge <na**@vulcan.la n>
wrote:
>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.
An insufficient quantity of initializers is specifically allowed
(6.7.8-19 and -21). Compilers are allowed to issue diagnostic
messages for correct code. Some do so when assigning an int to a char
or when using gets. These are not so much warnings (a term not
defined in the standard) but more like reminders to the programmer
that something is "unusual" and worth a second look. As long as the
code complies with the standard, the compiler is obligated to accept
it.
> 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**********@ news.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
It is portable, for both aggregate and scalar objects. See 6.7.8-16,
-11, and -19.
>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).
{0} should suffice. If the object requires non-zero initialization,
the library should provide an initialization function.
--
Remove del for email
Eric Sosman <Er*********@su n.comwrites:
Nate Eldredge wrote:
>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'.
In C99 you could use a "compound literal:"
struct foo blah = (struct foo){.a = 17, .b = 23};
(Double-check my syntax; I'm typing this hurriedly.)
My compiler says "error: initializer element is not constant". It
does work if it is moved inside a function, so that blah is auto, but
that's of course entirely different. (Anyway, in that case we could
just initialize blah.a and blah.b by hand.)
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. :)
Thanks to all who replied.
On Oct 2, 12:05 am, Nate Eldredge <n...@vulcan.la nwrote:
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?
Whoops, I forgot to reply to this question at my post.
In C99 you can.
struct foo blah = { .a = 17, .b = 23 };
Mr Sossman mentioned compound literals, but I'm unsure why he didn't
mention this which is even simpler.
You can also do this for array elements
char foo[2] = { .[1] = 'a' };
On Oct 2, 1:28 am, Eric Sosman <Eric.Sos...@su n.comwrote:
....
In C99 you could use a "compound literal:"
struct foo blah = (struct foo){.a = 17, .b = 23};
(Double-check my syntax; I'm typing this hurriedly.)
I think you mean
struct foo blah = *(struct foo[]){ { .a = 17, .b = 23 } };
Nate Eldredge <na**@vulcan.la nwrites:
Eric Sosman <Er*********@su n.comwrites:
>Nate Eldredge wrote:
>>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'.
In C99 you could use a "compound literal:"
struct foo blah = (struct foo){.a = 17, .b = 23};
(Double-check my syntax; I'm typing this hurriedly.)
Not allowed, I think, at file scope.
My compiler says "error: initializer element is not constant".
Just use the "initialise r list" rather than providing a compound
literal. I.e. write:
struct foo blah = {.a = 17, .b = 23};
This works for file-scope declarations as well as block-scope ones.
It
does work if it is moved inside a function, so that blah is auto, but
that's of course entirely different.
Yes. 6.7.8 paragraph 13 permits an expression of "compatible
structure or union type" when the object has automatic storage. You
have to read though to paragraph 16 to get to the text:
"Otherwise, the initializer for an object that has aggregate or
union type shall be a brace-enclosed list of initializers... "
to see that a compound literal is not allowed as the sole "top-level"
initialiser for an object with static storage duration. At least that
is my explanation of your error message.
--
Ben. vi******@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' };
--
Ben. vi******@gmail. com wrote:
[...]
Whoops, I forgot to reply to this question at my post.
In C99 you can.
struct foo blah = { .a = 17, .b = 23 };
Mr Sossman mentioned compound literals, but I'm unsure why he didn't
mention this which is even simpler.
Because I was (as I said) in a hurry, typing faster than
I was thinking. An insalubrious habit ...
--
Eric Sosman es*****@ieee-dot-org.invalid This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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.
|
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:
|
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
|
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.
| |
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;
};
|
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}
|
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,...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |