Search code examples
ctypeslanguage-lawyer

Clarification of "The meaning of a value is determined by the type of the expression used to access it"


From §6.2.5(1) of the standard:

The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. (An identifier declared to be an object is the simplest such expression; the type is specified in the declaration of the identifier.)

I am confused by the second sentence, specifically this bit: "An identifier declared to be an object is the simplest such expression" - how is this an example of what the first sentence is saying? What object is being accessed when we declare an identifier to be an object? Also, isn't this a declaration rather than an expression?

When I read the first sentence, I thought it was referring to a scenario such as the following:

int x = 3.2;        // `float` object is interpreted as an `int`

Here, the float object 3.2 is accessed by an assignment expression of type int and therefore the float object is interpreted as an int. Is this what the first sentence in the quote is saying?


Solution

  • Is this what the first sentence in the quote is saying?

    No.

    Given float f = 1.f/3;, then printf("%g\n", f); interprets the bytes of f as if they are encoded with the scheme for the float type. The value printed will be the value determined using that scheme.

    In contrast, printf("%hhu\n", * (unsigned char *) &f); interprets the first byte of f as if it is encoded using the scheme for unsigned char. Because the type used for access is unsigned char, the value of * (unsigned char *) &f will be the value determined using the scheme for unsigned char.

    how is this an example of what the first sentence is saying? What object is being accessed when we declare an identifier to be an object?

    When an expression is evaluated and there is an identifier for an object in the expression and the identifier is evaluated (e.g., is not the operand of sizeof), then the object identified by the identifier is accessed.

    Also, isn't this a declaration rather than an expression?

    It does not mean a declaration is an expression. It means that, after an identifier has been declared as an identifier for an object, that identifier in an expression is an expression for accessing the object.