指针诺塔顿有什么问题
what is wrong with pointer notaton
我面临的问题是,当我使用指针表示法并运行代码时,它不显示任何内容,当我使用数组表示法时,它会显示期望的结果。我不知道指针符号有什么问题。
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
char str[30] = "PROGRAMMING IS FUN";
char* ptr = str;
/*
int count=0;
while(ptr[count] != ' ')
{
ptr[count] = tolower(ptr[count]);
count++;
}
cout<<ptr<<endl; // result is displaying
*/
while(*ptr != ' ')
{
*ptr = tolower(*ptr);
ptr++;
}
cout<<ptr<<endl; // nothing is displaying also no compiler error
// ptr[0] and ptr[1] als0 displays nothing.
}
当while
循环中断时,ptr
指向字符串末尾的零字节。这就是while (*ptr != ' ')
所做的。所以当你尝试输出ptr
时,你正在输出一个空字符串。改为输出str
。
你必须打印"str"而不是"ptr",因为ptr指向str的末尾。
cout<<str<<endl;
但是,最好执行以下操作:
int len = strlen(str);
for(int i = 0; i<len; i++){
str[i] = tolower(str[i]);
}
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了