指针
详见: https://www.nowcoder.com/practice/8f5b923683b94e549880e3c8370e3e55?tpId=225&tags=&title=&difficulty=0&judgeStatus=0&rp=0
#include <iostream>
using namespace std;
int main() {
char str[30] = { 0 };
cin.getline(str, sizeof(str));
int m;
cin >> m;
char copyStr[30] = { 0 };
char* strPtr = str + m - 1;
char* copyStrPtr = copyStr;
while (*strPtr != NULL) {
*copyStrPtr = *strPtr;
strPtr++;
copyStrPtr++;
}
cout << copyStr << endl;
return 0;
}
用string
#include <iostream>
using namespace std;
int main() {
char str[30] = { 0 };
cin.getline(str, sizeof(str));
int m;
cin >> m;
char* p = str;
string s = p + m -1;
cout << s << endl;
return 0;
}
用&
这个方法…不算懂。。。
#include <iostream>
using namespace std;
int main() {
char str[30] = { 0 };
cin.getline(str, sizeof(str));
int m;
cin >> m;
char *p = NULL;
p = &str[m-1];
cout << p << endl;
return 0;
}
|