什么字符串数组[] ="英寸;意思是,为什么它的工作原理? [英] What does string array[] = ""; mean and why does it work?

查看:125
本文介绍了什么字符串数组[] ="英寸;意思是,为什么它的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string array[] = "";

我怎么能在一个为const char * 分配到一个数组?是一样的:

How am I able to assign a const char* to an array? Is that the same as:

string array[] = {""};

?这将是有意义的我。然而,这仍然无法正常工作

?? That would make sense to me. However, this still doesn't work

int array[] = 5;

那么究竟是什么,它​​不适合 INT 阵列解决它们之间的区别?

So what is the difference between them that it doesn't work for int arrays?

推荐答案

这是不正确code;它是在编译器中的错误(可能GCC / G ++?)去接受它。

This is incorrect code; it is a bug in your compiler (possibly gcc/g++?) to accept it.

铛提供了以下错误(<一个href=\"http://gcc.godbolt.org/#%7B%22version%22:3,%22filterAsm%22%3a%7B%22labels%22%3atrue,%22directives%22%3atrue,%22commentOnly%22%3atrue%7D,%22compilers%22%3a%5B%7B%22source%22%3a%22#include%20%3Cstring%3E%5Cnstd%3a%3astring%20a%5B%5D%20=%20%5C%22%5C%22;%5Cn%22,%22compiler%22%3a%22/usr/bin/clang++%22,%22options%22%3a%22-O2%22%7D%5D%7D\"相对=nofollow>链接):

clang gives the following error (link):

a.cpp:5:17: error: array initializer must be an initializer list
    std::string array[] = "";
                ^
1 error generated.

的Visual C ++同意(链接):

testvc.cpp(2) : error C3074: an array can only be initialized with an initializer-list

在该标准中的有关条款是8.5p17:

The relevant clause in the standard is 8.5p17:

[...]

   - 如果目标类型是一个字符数组,char16_t数组,char32_t数组或wchar_t数组,并初始化是一个字符串文字,见8.5.2。

  [...]

   - 否则,如果目标类型是一个阵列,在不良形成的程序。

  [...]

[...]
— If the destination type is an array of characters, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string literal, see 8.5.2.
[...]
— Otherwise, if the destination type is an array, the program is ill-formed.
[...]

提交给gcc作为一个bug: http://gcc.gnu.org /bugzilla/show_bug.cgi?id=60009

Submitted to gcc as a bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60009

这篇关于什么字符串数组[] =&QUOT;英寸;意思是,为什么它的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆