什么更有意义 - char* string 或 char *string?

2023-02-13C/C++开发问题
7

本文介绍了什么更有意义 - char* string 或 char *string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在学习 C++,并且遇到了很多以空字符结尾的字符串.这让我思考,在声明指针时什么更有意义:

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* 格式更有意义,因为字符串"的类型更有意义.是指向字符的指针,而不是字符.但是,我通常看到后一种格式.显然,这也适用于参考文献.

? 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 是一个字符指针,而 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6