什么更有意义 - char * string或char * string? [英] What makes more sense - char* string or char *string?
问题描述
我正在学习C ++,我遇到很多以null结束的字符串。这让我想,在声明指针时更有意义:
I'm learning C++ at the moment, and I'm coming across a lot of null-terminated strings. This has got me thinking, what makes more sense when declaring pointers:
char* string
或
char *string
?对我来说,char *格式更有意义,因为string的类型是一个指向char而不是char的指针。但是,我一般看到后者的格式。这也适用于引用。
? To me, the char* format makes more sense, because the type of "string" is a pointer to a char, rather than a char. However, I generally see the latter format. This applies to references as well, obviously.
有人可以告诉我后一种格式是否有逻辑原因?
Could someone tell me if there is a logical reason for the latter format?
推荐答案
在以下声明中:
char* string1, string2;
string1
是一个字符指针, code> string2 只有一个字符。为此,声明通常格式为:
string1
is a character pointer, but string2
is a single character only. For this reason, the declaration is usually formatted like:
char *string1, string2;
这使得更清楚的是 *
适用于 string1
,但不适用于 string2
。好的做法是避免在一个声明中声明多个变量,特别是如果其中一些是指针。
which makes it slightly clearer that the *
applies to string1
but not string2
. Good practice is to avoid declaring multiple variables in one declaration, especially if some of them are pointers.
这篇关于什么更有意义 - char * string或char * string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!