偶然地,我发现该行char s[] = {"Hello World"};
已经正确编译,似乎被视为相同char s[] = "Hello World";
.第一个({"Hello World"}
)不是包含一个char数组的元素的数组,所以s的声明应该读取char *s[]
吗?事实上,如果我将其更改为char *s[] = {"Hello World"};
编译器,也会按预期接受它.
寻找答案,我找到的唯一提到这个的地方是这个,但没有引用标准.
所以我的问题是,char s[] = {"Hello World"};
尽管左侧是类型array of char
而右侧是类型,为什么要编译该行array of array of char
?
以下是一个工作计划:
#include<stdio.h>
int main() {
char s[] = {"Hello World"};
printf("%s", s); // Same output if line above is char s[] = "Hello World";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何澄清.
PS我的编译器是gcc-4.3.4.
我有如下定义的查找表,我正在使用GCC.当我编译时,我收到警告
warning: braces around scalar initializer
Run Code Online (Sandbox Code Playgroud)
这个警告意味着什么?我该如何初始化这个LUT?我在初始化这个结构时犯了错误吗?
救命!!
typedef struct TECH
{
float velocity1, velocity2;
float temp;
float measure;
int id;
float storage[64];
}TECH;
struct TECH lut_model_1[2] = {{{296.001465},
{74.216972},
{2.025908},
{1.516384},
{1},
{0.001746,
0.000256, 0.006216, 0.005249, -0.001668, -0.001377, 0.009865, 0.010454, -0.000288, -0.005853, 0.010584, 0.015440, 0.000465, -0.000602, 0.004330, 0.005700, 0.017120,
0.233015, 0.034154, 0.244022, 0.007644, 0.385683, 0.042960, 0.406633, -0.007811, 0.346931, 0.040123, 0.387361, 0.007030, 0.225309, 0.017897, 0.241024, 0.003700,
0.103601, 0.060748, 0.121059, -0.045041, 0.076974, 0.070647, 0.148810, -0.022399, 0.074007, 0.054797, 0.141794, 0.010376, 0.052482, 0.045013, …
Run Code Online (Sandbox Code Playgroud) 我一直以为当我使用初始化列表C++语法时:
something({ ... });
Run Code Online (Sandbox Code Playgroud)
编译器总是清楚我想要调用过载std::initializer_list
,但对于MSVC 2015来说似乎并不那么清楚.
我测试了这个简单的代码:
#include <cstdio>
#include <initializer_list>
namespace testing {
template<typename T>
struct Test {
Test() {
printf("Test::Test()\n");
}
explicit Test(size_t count) {
printf("Test::Test(int)\n");
}
Test(std::initializer_list<T> init) {
printf("Test::Test(std::initializer_list)\n");
}
T* member;
};
struct IntSimilar {
int val;
IntSimilar() : val(0) {}
IntSimilar(int v) : val(v) {}
operator int() {
return val;
}
};
}
int main() {
testing::Test<testing::IntSimilar> obj({ 10 });
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并且在GCC 6.3中它按预期工作,呼叫 Test::Test(std::initializer_list)
但在MSVC 2015中,此代码调用Test::Test(int)
.
似乎MSVC可以以某种方式忽略 …
根据问题string array [] =""是什么; 意思是,它为什么有效?我想问一下下面代码中s1和s2之间的区别:
int main() {
const char* s1 = { "Hello" }; // strange but work as followed
const char* s2 = "Hello"; // ordinary case
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么允许额外的花括号?任何对C++标准的引用都会很有用.