C语言 如何遍历由指针创建的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18993591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop through a string created by pointer
提问by Lucy Luo
What I want to do is to iterate through the quote till the end of the quote/(*quote has nothing in it). Is my code valid?
我想要做的是遍历引用直到引用结束/(*quote 中没有任何内容)。我的代码有效吗?
char *quote = "To be or not to be, that is the question.";
for (quote = 0; *quote != NULL; quote++){
*quote = tolower(*quote);
}
回答by Bernhard Barker
You probably need another pointer to traverse the array, otherwise access to your original string will be lost.
您可能需要另一个指针来遍历数组,否则将无法访问原始字符串。
And preferably only use NULLfor pointers.
最好只NULL用于指针。
Don't use 0as the initial value, unless you want to use indices instead (see below).
不要0用作初始值,除非您想改用索引(见下文)。
Doing char *quote =will simply make quotepoint to the read-only literal, instead of copying the string. Use char quote[] =instead.
这样做char *quote =只会quote指向只读文字,而不是复制字符串。使用char quote[] =来代替。
char quote[] = "To be or not to be, that is the question.";
char *quotePtr;
for (quotePtr = quote; *quotePtr != 'char quote[] = "To be or not to be, that is the question.";
int i;
for (i = 0; quote[i] != 'char *quote = "Hello World";
'; i++){
quote[i] = tolower(quote[i]);
}
'; quotePtr++){
*quotePtr = tolower(*quotePtr);
}
Test.
测试。
Using indices:
使用索引:
Here *quote points to 'H'
BUT, you cannot do *quote = 'A';
This will give you an error.
Test.
测试。
回答by nikoo28
Consider this as an expansion to the answer given by Dukeling
When you use
当你使用
char quote[] = "Hello World";
Here also *quote points to 'H'
BUT, in this case *quote = 'A' is perfectly valid.
The array quote will get changed.
This makes a read-only string, means that you can't change its contents in a simpler way.
这使只读字符串,意味着您不能以更简单的方式更改其内容。
wchar const_t* quote = L"To be or not to be, that is the question.";
for( wchar_t* c = quote; *c != 'wchar const_t quote[] = L"To be or not to be, that is the question.";
for( size_t i = 0; i < sizeof(quote); i++ ) {
quote[i] = tolower( quote[i] );
}
'; c++ ) {
*c = tolower( *c );
}
If you wish to make changes to the characters in a string, it is a good habit to use arrays.
如果你想改变字符串中的字符,使用数组是一个好习惯。
##代码##回答by Dai
You're reassigning quotein your forinitializer, which is invalid and will cause an access-violation because you're dereferencing it in the *quote != NULLpart.
您quote在for初始化程序中重新分配,这是无效的并且会导致访问冲突,因为您在*quote != NULL部分中取消引用它。
Semantically NULLand '\0'are equivalent, but syntactically I'd prefer this. Note that by using this approach you keep a pointer to (the start of) the string.
在语义上NULL和'\0'是等价的,但在语法上我更喜欢这个。请注意,通过使用这种方法,您可以保留一个指向字符串(开头)的指针。
alternatively using an index:
或者使用索引:
##代码##(note that the semantics of sizeofwill change if the value of quoteis not known at compile time)
(请注意,sizeof如果quote在编译时不知道的值, 的语义将发生变化)

