Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15016

Re: const fields in a struct

From Nobody <nobody@nowhere.com>
Subject Re: const fields in a struct
Date 2011-12-27 03:16 +0000
Message-Id <pan.2011.12.27.03.16.05.263000@nowhere.com>
Newsgroups comp.lang.c
References <jdah0l$las$1@nnrp.ngi.it>
Organization Zen Internet

Show all headers | View raw


On Mon, 26 Dec 2011 20:13:27 +0100, pozz wrote:

> but, is it the correct way to solve my problem?

Yes. With "int *visible", the compiler won't complain if you do:

	*lbl1.visible = 0;

even though that would (attempt to) modify visible_true.

> What if I will have with a completely "non-const" label 
> when I want to change the visible status?  I won't be able to do 
> something like:
> 
>    int visible_lbl3;
>    LABEL lbl3 = { 0x0001U, "Non-const label", 1, &visible_lbl3};
>    *lbl3.visible = 0;

Correct; you'd have to change visible_lbl3 directly rather than through
the pointer.

If you're using C99, you could make the visible field a union, i.e.:

	union {
		const int *constant;
		int *mutable;
	} visible;

But then you'd need a tag field to indicate which of the two members was
used for a particular label. And that won't work with C89, which only
allows you to initialise the first element of a union.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

const fields in a struct pozz <pozzugno@gmail.com> - 2011-12-26 20:13 +0100
  Re: const fields in a struct Seebs <usenet-nospam@seebs.net> - 2011-12-26 20:26 +0000
    Re: const fields in a struct Philip Lantz <prl@canterey.us> - 2011-12-26 14:21 -0800
  Re: const fields in a struct Philip Lantz <prl@canterey.us> - 2011-12-26 14:22 -0800
  Re: const fields in a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-27 00:15 +0000
    Re: const fields in a struct pozz <pozzugno@gmail.com> - 2011-12-28 01:35 -0800
  Re: const fields in a struct Nobody <nobody@nowhere.com> - 2011-12-27 03:16 +0000

csiph-web