Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
use una tabla en lugar de cambiar la caja en C

¿Cómo reemplazar la caja del interruptor por una mesa en este caso, por favor?

 typedef enum { DIV_1 = 1, DIV_2, DIV_4 = 4, DIV_8 = 8, DIV_16 = 16 } eDiv_t; eDiv_t Division; uint32_t dividerValue; switch (Division) { case DIV_1: dividerValue = RCC_DIV_1; break; case DIV_2: dividerValue = RCC_DIV_2; break; case DIV_4: dividerValue = RCC_DIV_4; break; case DIV_8: dividerValue = RCC_DIV_8; break; case DIV_16: dividerValue = RCC_DIV_16; break; default: dividerValue = RCC_DIV_1; break; }

No sé cómo afectar las variables de enumeración a una matriz y usarla en lugar de cambiar el caso

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede definir su enumeración como valores enteros consecutivos e indexar su matriz con esta enumeración:

 typedef enum { DIV_1 = 0, DIV_2, DIV_4, DIV_8, DIV_16, NB_DIV } eDiv_t; unsigned int dividerValue[NB_DIV] = {RCC_DIV1, RCC_DIV2, ..., RCC_DIV16};
over 4 years ago · Santiago Trujillo Report

0

En este caso específico donde eDiv_t no se puede cambiar porque se usa en otro lugar, se puede hacer una tabla de búsqueda como:

 RCC_t eDiv_to_RCC (eDiv_t ediv) { static const RCC_t lookup [16+1] = { [DIV_1] = RCC_DIV1, [DIV_2] = RCC_DIV2, [DIV_4] = RCC_DIV4, [DIV_8] = RCC_DIV8, [DIV_16] = RCC_DIV16, }; return lookup[ediv]; // returns 0 if invalid input }

Esto es muy rápido pero consume 17 * sizeof(RCC_t) que supongo es otra enumeración, probablemente de 8 a 32 bits de tamaño. No es tan grande, pero esta es una velocidad de ejecución superior a la optimización del tamaño de .rodata. Por otro lado, el código real ocupará mucho menos espacio que la versión Switch.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!