如何通过创建的指针一串环 [英] How to loop through a string created by pointer

查看:160
本文介绍了如何通过创建的指针一串环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是通过引用遍历到报价/结束时(*报价有什么也没有)。是我的code有效?

 的char *报价=生存还是毁灭,这是个问题。
对于(报价= 0;!*报价= NULL;报价++){
*报价= tolower的(*报价);
}


解决方案

您可能需要另一个指针遍历数组,否则访问原始的字符串将会丢失。

和preferably只使用 NULL 为指针。

不要使用 0 作为初始值,除非你想用指数来代替(见下文)。

的char *报价= 只会让报价指向只读字面,而不是复制的串。使用引用的char [] = 代替。

 引用的char [] =生存还是毁灭,这是个问题。
字符* quotePtr;
对于(quotePtr =报价;!* quotePtr ='\\ 0'; quotePtr ++){
  * quotePtr = tolower的(* quotePtr);
}

测试

使用索引:

 引用的char [] =生存还是毁灭,这是个问题。
INT I;
对于(i = 0;!报价由[i] ='\\ 0';我++){
  报价由[i] = tolower的(报价由[i]);
}

测试

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?

char *quote = "To be or not to be, that is the question.";
for (quote = 0; *quote != NULL; quote++){
*quote = tolower(*quote);
}

解决方案

You probably need another pointer to traverse the array, otherwise access to your original string will be lost.

And preferably only use NULL for pointers.

Don't use 0 as the initial value, unless you want to use indices instead (see below).

Doing char *quote = will simply make quote point to the read-only literal, instead of copying the string. Use char quote[] = instead.

char quote[] = "To be or not to be, that is the question.";
char *quotePtr;
for (quotePtr = quote; *quotePtr != '\0'; quotePtr++){
  *quotePtr = tolower(*quotePtr);
}

Test.

Using indices:

char quote[] = "To be or not to be, that is the question.";
int i;
for (i = 0; quote[i] != '\0'; i++){
  quote[i] = tolower(quote[i]);
}

Test.

这篇关于如何通过创建的指针一串环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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