在c ++中遇到奇怪的类型 [英] encountered strange type in c++

查看:46
本文介绍了在c ++中遇到奇怪的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思?



What does this mean?

char const *  pWhiteList[] =
{
    "str1",
    "str2",
};





我知道 char * 意味着什么。



我也知道 char someVal [50] 意味着什么。



但是我从未见过如上所述的定义。



欢迎任何解释。



PS。我认为类似的构造在C ++中主要遇到,例如,





I know what char * means.

I also know what char someVal[50] means.

But I have never seen them defined together, like above.

Any explanations welcome.

PS. I think similar construct one encounters in main in C++, e.g.,

int _tmain(int argc, _TCHAR* argv[])





上面我们有 char * [] 的组合:

_TCHAR* argv[]



这是什么意思?

.
So what does this mean?

推荐答案

char const *  pWhiteList[] =



从右到左阅读:


Reading right to left:

pWhiteList[] : is an array
*            : of pointers
const        : to constant
char         : characters


它声明一个常量指针到字符的数组并初始化它。

In换句话说,它会创建一个包含两个字符串的数组,并将其分配给pWhiteList。
It declares an array of constant pointer-to-characters and initialises it.
In other words it creates an array of two strings, and assigns it to pWhiteList.


读取C / C ++声明可能很难。

我也遇到了麻烦,直到最近我学会了可靠地阅读这些。



从实体名称开始(例如 x )并执行以下操作:

Reading C/C++ declaration may be hard.
I had my troubles too until lately I learned to read these reliably.

Start at the entity name (e.g. x) and do the following:


  1. 声明x为
  2. 直接跟随(...) [...] 函数(...)分别返回数组[...]
  3. 所有直接前缀指针运算符(例如 * )说指向(添加const,volatile和其他指针运算符)
  4. 如果以上嵌套(例如(* x))继续2。
  5. 剩下的是类型,所以你说type

  1. "declare x as "
  2. for all the directly following (...) and [...] say "function (...) returning " and "array [...] of " respectively
  3. for all directly prefixing pointer operators (e.g. *) say "pointer to " (add const, volatile and other pointer operators)
  4. if the above was nested (e.g. (*x)) continue a 2.
  5. what remains is the type, so you say "type"





例如



int x 声明x为 int
int * x 将x声明为 指向 int
const char * x [4] 声明x为 数组[4]的 指针to const char
const char(* x)[4] 声明x为 指向
long * f(double) 声明x为 函数(double)返回 指向 long
long(* f)(double) 声明x为 指向 函数(double)返回 long




干杯

Andi



E.g.

int xdeclare x as int
int *xdeclare x as pointer to int
const char *x[4]declare x as array [4] of pointer to const char
const char (*x)[4]declare x as pointer to array [4] of const char
long *f(double)declare x as function (double) returning pointer to long
long (*f)(double)declare x as pointer to function (double) returning long


Cheers
Andi


这篇关于在c ++中遇到奇怪的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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