试图理解一些c++;向后遍历字符数组的代码 我正在遵循一个C++教程,我试图通过每一条线,但我被难住了。到目前为止,我有一个char数组,我正在尝试使用指针和while循环向后迭代。我添加了一些评论,试图了解每一点都在做什么。有人能证实我的
试图理解一些c++;向后遍历字符数组的代码 我正在遵循一个C++教程,我试图通过每一条线,但我被难住了。到目前为止,我有一个char数组,我正在尝试使用指针和while循环向后迭代。我添加了一些评论,试图了解每一点都在做什么。有人能证实我的理解吗试图理解一些c++;向后遍历字符数组的代码 我正在遵循一个C++教程,我试图通过每一条线,但我被难住了。到目前为止,我有一个char数组,我正在尝试使用指针和while循环向后迭代。我添加了一些评论,试图了解每一点都在做什么。有人能证实我的,c++,C++,试图理解一些c++;向后遍历字符数组的代码 我正在遵循一个C++教程,我试图通过每一条线,但我被难住了。到目前为止,我有一个char数组,我正在尝试使用指针和while循环向后迭代。我添加了一些评论,试图了解每一点都在做什么。有人能证实我的理解吗 #include <iostream> using namespace std; int main() { char text[] = "hello"; int nChars = sizeof(text)
#include <iostream>
using namespace std;
int main()
{
char text[] = "hello";
int nChars = sizeof(text) - 1; // finds the size of the array (equals 6), and subtracts 1. "Hello" has 5 letters, but there's an added 6th element called the stop element.
cout << nChars << endl;
char* pStart = text; // pStart will Point to first value in array.
char* pEnd = text + nChars - 1; // takes the first element in the array, adds the amount of additional elements, and subtracts 1 from the end.
while (pStart < pEnd)
{
char save = *pStart; // Save the first element (h) in "hello".
*pStart = *pEnd; // Takes the beginning element (h), adds the remaining elements, removes the termination character (nChars - 1) then sets the end as the beginning
pStart++; // Increment through the char array with the end as the beginning, working through 'o', increment to 'l', then 'l', then 'e', then 'h'.
pEnd--; //
}
return 0;
}
#包括
使用名称空间std;
int main()
{
字符文本[]=“你好”;
int nChars=sizeof(text)-1;//查找数组的大小(等于6),然后减去1。“Hello”有5个字母,但添加了第6个元素,称为stop元素。
是否缺少的部分是在*pStart=*pEnd;
之后如何处理save
,text+nChars-1;
中的-1
与int-nChars=sizeof(text)-1;
中的似乎是多余的。我建议添加"<代码> >行>代码> *pStase**pEnter;< /Cord>不添加或删除任何字符串。该代码不通过字符串反向迭代。我在遵循C++教程,我试图通过每一行来遍历。如果您知道如何使用调试器,可以一次执行代码1行,然后查看变量的变化。每行后面都有一个空格。