|
老师,你好。教学内容中的第四点 有些疑惑
用指针操作字符串数组:
定义两个字符数组输入如下:
char str1[50] = {0}, str2[] = "I Love cctry.com!";
用指针的方式,实现将 str2 拷贝到 str1 中。
// P23.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char str1[50] = { 0 }, str2[] = "I Love cctry.com!";
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2 << endl;
char* pstr1 = str1, *pstr2 = str2;
int lenstr2 = strlen(str2);
for (int idx = 0; idx < lenstr2; idx++)
{
//str1[idx] = str2[idx];
pstr1[idx] = pstr2[idx];
}
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2 << endl;
return 0;
}
循环语句中的 “pstr1[idx] = pstr2[idx]” 有些不明白,为什么这里能像数组一样使用? 谢谢!
不是定义了这个吗?
- char* pstr1 = str1, *pstr2 = str2;
复制代码
pstr1指向数组str1的首地址,相当于pstr1[]这种形式
pstr2指向数组str2的首地址,相当于pstr2[]这种形式
不知道这种解释行不行?
|
上一篇: 指针问题求助怎么简写下一篇: 不知道是怎么来写,,好多都变了
|