裸字符串数组等效 [英] Array Equivalent of Bare-String

查看:185
本文介绍了裸字符串数组等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样做没有问题:

I can do this without issue:

const char* foo = "This is a bare-string";

我要的是能够做同样的事情用一个数组:

What I want is to be able to do the same thing with an array:

const int* bar = {1, 2, 3};

显然,这code不编译,但有一些类型的数组相当于裸字符串?

Obviously that code doesn't compile, but is there some kind of array equivalent to the bare-string?

推荐答案

您不能做到这一点:

const int* bar = {1, 2, 3};

但你可以这样做:

But you can do this:

const int bar[] = {1, 2, 3};

原因是C(或C ++)有一个附加的功能,除了作为一个字符指针工作,它也可以作为一个C字符串,从而增加初始化方法(特别对于char *)的字符*:

Reason is that char* in C (or C++) have an added functionality, besides working as a char pointer, it also works as a "C string", thus the added initialization method (special for char*):

const char* foo = "This is bare-string";

最好的。

这篇关于裸字符串数组等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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