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

331
Visualizações
Understanding sizeof struct in C program

I have defined in my C program a struct as shown below:

typedef struct {
    long time_of_arrival;           // Number of minutes after the salon opening when the customer arrived
    char customer_name[21];         // Upto 20 characters long in all caps
    char stylist_name[21];          // Preferred stylist name, upto 20 characters long or NONE, in all caps
    long loyalty_points;            // The loyalty points customer holds
    long requested_haircut_time;    // The time corresponding to the haircut customer requested
    long loyalty_points_earned;     // The loyalty points customer earned
} customer;

I wish to create an array of structures of size per the number of customers as supplied by the user via command line argument.

To determine the amount of memory a single instance of the said structure would occupy in memory, I ran the following snippet of code:

int main(int argc, char const *argv[]) {
    printf("%ld\n", sizeof(customer));

    return 0;
}

which printed the following output:

80

However, in my limited understanding, the size should be 74 bytes, i.e. 8 bytes for each long variable and 21 x 2 bytes for the two character arrays.

Upon further debugging, (i.e. by creating a standalone array of 21 characters and checking its size using sizeof operator, which got reported as 21 bytes), I concluded that for some reason, the size of character array under the structure was getting rounded up to the multiple of 8 bytes.

I need help in understanding what phenomenon is responsible for this behaviour and what is it that I am missing here?

Note: I am building the C program using Apple clang version 13.0.0 (clang-1300.0.29.3) running on macOS 12.0.1 (21A559).

over 4 years ago · Santiago Trujillo
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