gpt4 book ai didi

c++ - 在 C++ 中使用指针进行数组索引

转载 作者:行者123 更新时间:2023-11-30 02:48:59 25 4
gpt4 key购买 nike

<分区>

虽然复制数组工作得很好,但程序没有打印原始数组……有人能给我解释一下 while 循环在这里是如何工作的吗(我是指针的新手) ……

// Copy an array in C++ using pointers

#include<iostream.h>
using namespace std;

void main()
{
char name[] = {'S','P','A','R','T','A','N'};
char copy[7];
char *p1, *p2;
p1 = name;
p2 = copy;
while(*p1) *p2++ = *p1++;
*p2 = 0;
cout<<"Here is the original name array:\n";
p1 = name;
cout<<name<<"\n";
while(*p1) cout<< *p1++ <<" ";
cout<<endl;
cout<<"Here is the copy:\n";
p1 = copy;
while(*p1) cout<< *p1++ <<" ";
cout<<endl;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com