0

I trying create C-struct with switch. I have a enum with values of type of messages in TLS Handshake protocol.

  typedef struct ui24 { int uint24[3] } uint24;

  typedef enum HT {
    client_hello = 0x01,
    server_helo = 0x02,
    new_session_ticket = 0x04,
    end_of_early_data = 0x05,
    encrypted_extensions = 0x08,
    setrificate = 0x0B,
    sertificate_request = 0x0D,
    certificate_verify = 0x0F,
    finished = 0x14,
    key_update = 0x18,
    message_hash = 0xFE
  } HandshakeType;

I need compare real message type with each value in my list of message type for understanding, that my message is ClientHello or ServerHello or another. Operator "switch" is underlining and have an error "expected a type specifier".

struct Handshake {
    HandshakeType msg_type;
    uint24 lenght;
    switch (msg_type)
    {
    case client_hello: char* ClientHello
        break;
    case server_hello: char* ServerHello
        break;
    };
};
aashney
  • 1
  • 1
  • 2
    `switch` is a control statement, it can only be used in executable code, not in struct declarations. – Barmar Sep 21 '20 at 15:04
  • It looks like you're trying to implement a discriminated union. This can't be done in the declaration, it has to be done in the code that uses the structure. – Barmar Sep 21 '20 at 15:05
  • @Barmar Ah, isn't this the *exact* difference in classes-objects paradigm offered by OOP ? (I don't know oop, so curious :p ) – A P Jo Sep 21 '20 at 16:19
  • @APJo There's some similarity with polymorphic classes. – Barmar Sep 21 '20 at 16:20
  • @Barmar Aha, and i will learn oop to understand what that is :) – A P Jo Sep 21 '20 at 16:21

0 Answers0