简体   繁体   English

C字符串类型定义

[英]C string type definition

I was just trying to make a string like type when you could just write: 我只是想写一个像字符串类似的字符串:

string s;

And I thought to do it like: 我想这样做:

#define string char *

Then for example in main function I have to write only string s; 然后例如在main函数中我只需要写string s; and if I type different variable name then it doesn't work. 如果我键入不同的变量名称,那么它不起作用。

How can I improve the definition or maybe how can I use "typedef" for this job if it's not a bad practice to do it so. 我如何改进定义,或者如果这样做的话,我怎么能用“typedef”来完成这项工作。 Or is there any better approach to make variables type of string? 或者有更好的方法来制作变量类型的字符串吗?
I was searching but I think I couldn't find the answer. 我在搜索,但我想我找不到答案。

You can't make variables of type string in C, because "string" is not a type. 不能在C中创建string类型的变量,因为“string”不是一个类型。

A "string" is, by definition, "a contiguous sequence of characters terminated by and including the first null character". 根据定义,“字符串”是“由第一个空字符终止并包括第一个空字符的连续字符序列”。 It's not a data type, it's a data format. 它不是数据类型,而是数据格式。

An array of char may contain a string. char数组可能包含一个字符串。 A char* may point to a string. char*可以指向一个字符串。 Neither of them is a string. 它们都不字符串。

If you like, you can define 如果您愿意,可以定义

typedef char *string; /* not recommended */

but that's misleading, since a variable of type char* , as I mentioned, isn't a string. 但这是误导,因为我提到的char*类型的变量不是字符串。

The best practice is simply to use char* directly. 最好的做法是直接使用char* This makes it clear that your variable is a pointer. 这清楚表明你的变量是一个指针。 It's also consistent with the way the standard library is defined; 它也与标准库的定义方式一致; for example, the strlen function is declared as: 例如, strlen函数声明为:

size_t strlen(const char *s);

It's also consistent with the way most experienced C programmers write code that deals with strings. 它也与大多数有经验的C程序员编写处理字符串的代码的方式一致。

Because of the way C treats arrays (more or less as second-class citizens), arrays, including arrays that contain strings, are usually manipulated via pointers to their elements. 由于C处理数组的方式(或多或少作为二等公民),数组(包括包含字符串的数组)通常通过指向其元素的指针进行操作。 We can use pointer arithmetic to traverse an array. 我们可以使用指针算法遍历数组。 Pretending that the pointer is the array, or that it is a string, is tempting, and might seem to make the code more understandable, but in the long run it just causes confusion. 假装指针数组,或者它一个字符串,是诱人的,似乎可以使代码更容易理解,但从长远来看,它只会引起混淆。

A macro approach like 像宏的方法

#define string char*

is even worse than a typedef . 甚至比typedef更糟糕。 Macros are expanded as sequences of tokens; 宏被扩展为令牌序列; the processor doesn't know about the syntax of C declarations. 处理器不知道C声明的语法。 So given the above definition, this: 所以鉴于上面的定义,这个:

string x, y;

expands to 扩展到

char* x, y;

which defines x as a char* and y as a char . 它将x定义为char* ,将y定义为char If you need a name for a type, typedef is almost always better than #define . 如果你需要一个类型的名字, typedef几乎总是比#define

You have to learn what the Preprocessor is. 您必须了解预处理器是什么。 As for your problem, the right solution is 至于你的问题,正确的解决方案是

typedef char *string;

and if you want to use the preprocessor remove the semi colon and the s like this 如果你想使用预处理器删除半冒号和这样的s

#define string char *

In the second case the preprocessor will replace each occurence of string with char * , and hence if you declare 在第二种情况下,预处理器将使用char *替换每个string出现,因此如果您声明

string x, y;

it will expand to 它会扩展到

char *x, y;

where x if a pointer to char and y is simply a char , this is misleading and should be avoided. 其中x如果指向chary的指针只是一个char ,这是误导性的,应该避免。

One more thing, when working in C it is never a good Idea to hide the fact that some variable is a pointer, so things like 还有一件事,当在C中工作时,隐藏一些变量是指针的事实绝不是一个好主意,所以就像这样

typedef SomeType *SomeTypeName;

where SomeType could be any type, are generally a bad idea, you can do something I've seen that clarifies this a little, you can append a P to SomeType , like this 其中SomeType可以是任何类型,通常是一个坏主意,你可以做一些我已经看到的澄清这一点的东西,你可以将P附加到SomeType ,就像这样

typedef SomeType *SomeTypeP;

but I personally prefer the * to any of these typedef s 但我个人更喜欢*到任何这些typedef

Don't. 别。 string is a C++ keyword. string是一个C ++关键字。 Not only it will cause problems if C++ becomes involved, but more importantly, it causes confusion and hides the fact that you are dealing with a pointer. 如果涉及到C ++,它不仅会引起问题,更重要的是,它会引起混淆并隐藏你正在处理指针的事实。

UPDATE: Right guys, it's not a keyword. 更新:合适的人,这不是关键字。 But if like many people you put using namespace std; 但是,如果你using namespace std;许多人using namespace std; in your code, then string is a C++ class from the standard library. 在您的代码中, string是标准库中的C ++类。

What I wanted to say is that it is not a common practice to make an alias for char * and it might confuse other people looking at your code. 我想说的是,为char *创建一个别名并不常见,它可能会让看到你代码的其他人感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM