Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

517
Visualizações
Can the C offsetof() macro be applied to unions?

It's often assumed that offsetof() can be applied to unions (you can even find such use in several questions here at SO), but, however, it seems like all the C specifications from C90 to nowadays only say the offsetof() macro supports structs. I was now taking a look at the "Modern C" book by Jens Gustedt, and table 4.3 specifies "struct" as the type of the first argument to offsetof().

So,... are unions officially supported by offsetof(), or not?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is actually a defect in the C spec, up until C2x. This was brought up in a defect report for the C11 spec DR 496, which was resolved in C2x.

From the C11 spec section §7.19 paragraph 3, the definition for offsetof:

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). The type and member designator shall be such that given

static type t;

then the expression &(t.member-designator) evaluates to an address constant. (If the specified member is a bit-field, the behavior is undefined.)

The type argument is described as a "structure member", yet the expression &(t.member-designator) is also well-defined for union types (§6.7.2.1 paragraph 16). This gave rise to ambiguity for compiler authors, so most have chosen to allow offsetof on unions.

In the C2x spec this is reworded (bold emphasis mine):

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the value of which is the offset in bytes, to the subobject (designated by member-designator), from the beginning of any object of type type. The type and member designator shall be such that given

static type t;

then the expression &(t.member-designator) evaluates to an address constant. If the specified type defines a new type or if the specified member is a bit-field, the behavior is undefined.

With the changes being:

  • "structure member" -> "subobject"
  • "beginning of its structure" -> "beginning of any object of type type"
  • using offsetof on new type declarations is undefined behavior

To answer the question of "can offsetof be applied to unions":

  • In C2x, yes
  • In older versions, likely also yes, given many compilers support it
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda