[comp.lang.c] Write-only variables

raph@tigger.planet.bt.co.uk (Raphael Mankin) (09/14/89)

How does one declare write-only variables in C? This is not as silly a
question as it sounds. I can declare read-only variables as 'const' or
'const volatile'; the latter corresponds to a read-only register in a
memory-mapped device. Now, 'const volatile' sounds like a contradiction
in terms, but that is how it is done.

If I have a write-only device register it would be a write-only
variable to C.

BTW some devices behave differently when one reads
them from when one writes them. Some UARTs, for instance, (6850 I
think) map the status register (read only) to the same location as a
control register (write only). One tells a compiler that the
value it just wrote to a location is ALWAYS different from the value it
will read back by the use of 'volatile'. 'Volatile' is not quite
strong enough here, but it is what we have.

Raphael Mankin
raph@planet.bt.co.uk
-- 
Raphael Mankin
raph@planet.bt.co.uk

flaps@dgp.toronto.edu (Alan J Rosenthal) (09/15/89)

raph@tigger.planet.bt.co.uk (Raphael Mankin) writes:
>How does one declare write-only variables in C?
[ explanation of the use of write-only variables deleted ]

You cannot write to a variable in C without having an lvalue expression for
that variable.
All lvalue expressions in C have meaning as rvalues.%
Therefore, you cannot declare write-only variables in C.
(I think :-))

You can't even fake it with a macro, I don't think.
(Like "#define x (x_real + 0)" for faking const.)

ajr

% humourously, in ansi C, not all lvalue expressions in C have meaning as
lvalues (namely, array names).

hascall@atanasoff.cs.iastate.edu (John Hascall) (09/15/89)

In article <...> flaps@dgp.toronto.edu (Alan J Rosenthal) writes:
}raph@tigger.planet.bt.co.uk (Raphael Mankin) writes:
}>How does one declare write-only variables in C?
}[ explanation of the use of write-only variables deleted ]
 
}You cannot write to a variable in C ...

    Come to think of it, why is there no "writeonly" modifier?

    It would seem useful, and orthoganal (sp?).

    John

rns@se-sd.NCR.COM (Rick Schubert ) (09/20/89)

In article <1989Sep14.183749.12753@jarvis.csri.toronto.edu> flaps@dgp.toronto.edu (Alan J Rosenthal) writes:
>% humourously, in ansi C, not all lvalue expressions in C have meaning as
>lvalues (namely, array names).

This is true if your definition of "lvalue" is something that can be assigned
to (i.e. appear as the left-hand side of an assignment operator);
however, this is not how ANSI C defines `lvalue'.  From 3.2.2.1:
______________________________________________________________________________
           An `lvalue' is an expression (with an object type or an
      incomplete type other than |void|) that designates an object.*
      When an object is said to have a particular type, the type is
      specified by the lvalue used to designate the object.
      A `modifiable lvalue' is an lvalue that does not have array
      type, does not have an incomplete type, does not have a
      const-qualified type, and if it is a structure or union,
      does not have any member (including, recursively, any member
      of all contained structures or unions) with a const-qualified
      type.
      ________________
      * The name "lvalue" comes originally from the assignment expression
        E1 = E2, in which the left operand E1 must be a (modifiable)
        lvalue.  It is perhaps better considered as representing an
        object "locator value."  What is sometimes called "rvalue" is in
        this Standard described as the "value of an expression."
      
        An obvious example of an lvalue is an identifier of an object.
        As a further example, if E is a unary expression that is a
        pointer to an object, *E is an lvalue that designates the object
        to which E points.
______________________________________________________________________________

-- Rick Schubert (rns@se-sd.sandiego.NCR.COM)