Matthew Smith
Matthew Smith

Reputation: 6255

Howto initialise char array style string in constructor

I have a class which I'm serialising to send over a unix socket and it has to have a string which I've stored as a char array. Can I initialise it in the constructor differently to how I've done it here?

typedef struct SerialFunctionStatus_t {
    SerialFunctionStatus_t() 
        : serial_rx_count(0), serial_tx_count(0), socket_rx_count(0), socket_tx_count(0) 
        { port[0] = '\0'; }
    uint32_t serial_rx_count;
    uint32_t serial_tx_count;
    uint32_t socket_rx_count;
    uint32_t socket_tx_count;
    char port[20];
} SerialFunctionStatus_t;

Upvotes: 2

Views: 1745

Answers (1)

fizzer
fizzer

Reputation: 13796

Put port() in the initializer list. This causes port to be 'value initialized' (12.6.2), which for arrays of builtins means zero initialized (8.5).

Upvotes: 7

Related Questions