How to properly initialize 'struct stat' using C ++ 11?

For many years, I initialized mine struct statas follows:

#include <sys/stat.h>
struct stat foo = {0};

In particular, that {0}sets all fields to zero, which is equivalent memset (&foo, NULL, sizeof foo);. Now with C ++ 11, this has led to warnings:

foo.cpp:2:19: warning: missing field 'st_mode' initializer [-Wmissing-field-initializers]
  struct stat s = {0};
                    ^

This is due to the new C ++ 11 initializer syntax, and the warning implies that I am not initializing all members. What is the preferred way to create and initialize struct statin C ++ 11?

+4
source share
3 answers

Use

stat s{};

- . - . struct stat foo = {0}; (, struct stat aggregate), , .

+8

struct s{}, C99: struct stat s = { .st_dev = 0 }; , , memset( &s, 0, sizeof(struct stat) ); .

+1

{0}? , 0 " ", ? " "... (, , - ).

struct stat foo = {} , , .

, , struct stat foo{};.

+1

Source: https://habr.com/ru/post/1611325/


All Articles