INITIALIZATION METHOD OF ARRAY OF c++

Asked 1 years ago, Updated 1 years ago, 31 views

when declaring char array
char buf[3]{};

I saw the code that says

In my opinion,

char buf[3]={0};

and so on, but if you look at the results, both methods are filled with zero and 3 bytes.

Is the code correct in terms of c++?
The compiler is gcc 4.4.7.

c++

2022-09-30 19:30

1 Answers

It depends on what you call "correct"...

This is the "initializer list" syntax introduced in .
https://cpprefjp.github.io/lang/cpp11/initializer_lists.html
- Braces {} can be empty
- Direct initialization does not write = (where = will cause copy initialization)
does not accept this initializer list.

In Oira's one-chip microcomputer development environment, none of the questions that correspond to are correct.Similarly, the relatively new gcc is the correct way to write.

In gcc-7.1.0, this behavior was shown.

$g++-ccpp11init.cpp#Compiled without warning
$ g++-c-std=c++03cppinit.cpp
cppinit.cpp:3:16:warning:extended initializer lists only available with-std=c++11 or-std=gnu++11
   char buf[3]{};
              ^
$

g++-ccpp11init.cpp alerts in gcc-4.9.4
g++-c-std=c++11cpp11init.cpp now has no warning.

You may or may not be correct or incorrect in speaking based on what year of language specification.View questions tagged with or


2022-09-30 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.