声明指针;星号的类型和名称之间的空间的左边还是右边? [英] Declaring pointers; asterisk on the left or right of the space between the type and name?

查看:732
本文介绍了声明指针;星号的类型和名称之间的空间的左边还是右边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:结果
  <一href=\"http://stackoverflow.com/questions/558474/what-makes-more-sense-char-string-or-char-string\">What更有意义 - 的char *字符串或字符*串?
  <一href=\"http://stackoverflow.com/questions/180401/pointer-declarations-in-c-placement-of-the-asterisk\">Pointer在C ++中声明:星号的位置结果
  

Possible Duplicates:
What makes more sense - char* string or char *string? Pointer declarations in C++: placement of the asterisk

我见过的这种混合版本有很多code的。 (这适用于C和C ++,顺便说一句。)人们似乎两种方式之一来声明指针,我不知道哪一个是正确的,如果它甚至事项。

I've seen mixed versions of this in a lot of code. (This applies to C and C++, by the way.) People seem to declare pointers in one of two ways, and I have no idea which one is correct, of if it even matters.

第一种方式是把星号相邻的类型名称,例如:

The first way it to put the asterisk adjacent the type name, like so:

someType* somePtr;

第二种方法是将星号相邻的名义变量,像这样:

The second way is to put the asterisk adjacent the name of the variable, like so:

someType *somePtr;

这已经现在我发疯了一段时间。是否有声明指针任何标准的方式?难道它甚至不管指针如何申报?我以前用过这两个声明,我知道,编译器不关心它是哪种方式。不过,我已经看到了两种不同的方式宣告指针的事实使我相信,有它背后的原因。我很好奇,如果这两种方法是在某些方面,我很想念更可读的或逻辑的。

This has been driving me nuts for some time now. Is there any standard way of declaring pointers? Does it even matter how pointers are declared? I've used both declarations before, and I know that the compiler doesn't care which way it is. However, the fact that I've seen pointers declared in two different ways leads me to believe that there's a reason behind it. I'm curious if either method is more readable or logical in some way that I'm missing.

推荐答案

这是preference的事,和圣战某种程度上,就像括号风格。

It's a matter of preference, and somewhat of a holy war, just like brace style.

样式

someType* somePtr;

时,强调指针变量的类型。这是说,从本质上讲, somePtr 的类型是指针到 - SOMETYPE

is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr is pointer-to-someType".

样式

someType *somePtr

时,强调指向的数据的类型。这是说,从本质上讲,数据的类型指向 somePtr SOMETYPE

is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr is someType".

他们都意味着同样的事情,但它取决于如果在创建一个指针是专注,可以这么说,在指向的数据或指针变量。一个给定的程序员的心智模型

They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.

把它在中间(如 SOMETYPE * somePtr )正在努力避免犯要么之一。

Putting it in the middle (as someType * somePtr) is trying to avoid committing to either one.

这篇关于声明指针;星号的类型和名称之间的空间的左边还是右边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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